A practical guide. Running Existing Word VBA Macros from C # Ribbon Addin

Reference Information. I have an extensive set of specialized VBA macros used in Word to format documents. In Word 2003, these macros were activated from a custom toolbar. I recently switched to Word 2007 and would like to be able to run these existing VBA macros from a new Word ribbon created using VS 2010. I created a ribbon; however, I cannot figure out how to call existing macros using the new ribbon buttons.

Question: How do I call existing VBA macros that are stored in a .dotm template from a C # Word add-in?

Any help would be greatly appreciated.

+4
c # vba word-vba vsto
source share
1 answer

The technique described in MS KB article 306683 - in particular, the RunMacro function defined there should allow you to call the VBA Macro from C # code: you define the RunMacro function

 private void RunMacro(object oApp, object[] oRunArgs) { oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs); } 

and then call your macro like this:

 RunMacro(oApp, new object[] {"NameOfMyMacro"}) 

or

 RunMacro(oApp, new object[] {"NameOfMyMacro", "some", 3, "parameters"}) 

oApp is a Word.Application object, which I'm sure is available somewhere in the Word add-in.

+4
source share

All Articles