[Philip]

Hello there!

I need to run a macro from my Excel 2003 extension project. I have no problem running existing macros that are in the workbook I develop extension for but when I go to macro editor and add my own new macro and then try to run it from .NET code I get a message "Macro not found".

Here's my macro

Public Sub MySub()
Dim a As String
a = "hello"
End Sub

Here's my .NET code

//this one runs fine
Globals.ThisWorkbook.Application.Run("moduleName.macroName", ....millions of Type.Missing);

//this one doesn't run
Globals.ThisWorkbook.Application.Run("moduleName.MySub", ....millions of Type.Missing);

I just added that macro to the Workbook, made sure that VBA code compiles. I was also able to run it from the Visual Basic editor.



Re: Visual Studio Tools for Office Running Macro from excel extension

Dennis Wallentin

Philip,

What are You trying to achieve with this approach






Re: Visual Studio Tools for Office Running Macro from excel extension

[Philip]

Well, I have ommitted some code in between macro calls. There's some .NET code executed after first macro call and before the second one. The idea is to use the second macro to format data from .NET and put it in corresponding ranges in the workbook. This is done to isolate data retrieving logic from formatting. There's a lot of code that is written in VBA inside the workbook I am working with and I want to keep it that way instead of rewriting all that using .NET





Re: Visual Studio Tools for Office Running Macro from excel extension

JocularJoe

Works fine for me.

Are you sure you are adding your macro to the right workbook

IIRC VSTO 2005 copies the workbook from the project folder to the bin folder during a build, then runs the copy in the bin folder when debugging. Is it possible you are adding your macro to the wrong file after the build has finished





Re: Visual Studio Tools for Office Running Macro from excel extension

[Philip]

@ JocularJoe

Thank you Joe. I am aware of that fact. The problem I am having is a very strange one. I know very little about VBA programming. Do you need to take any other steps like "registering" a macro before you can run it As I mentioned before, I am able to run that new macro from the Visual Basic editor but not from my .NET code.





Re: Visual Studio Tools for Office Running Macro from excel extension

[Philip]

I got it!

JocularJoe, your post made me start thinking in a bit different direction! My first macro does a lot of stuff ... and one of the things it does is creating a new workbook based on the template. It also makes that new workbook active so by the time I call my second macro, ThisWorkbook variable already points to the newly created Workbook which of course doesn't have the macro I need. Thanks for the hint, Joe!