I have a VSTO Add-in
for Microsoft Word
and I wish to embed a macro so it will be globally available from all VBA projects or other VSTO add-ins.
Is that possible? If so, how can it be done?
I hope it can be done without requiring Trust access to the VBA project object model
(info) since I don't like having to ask my customers to do that.
I know that macros can be executed from VSTO like this
You define a function
RunMacro
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 theWord.Application
object, which I'm sure is available somewhere in a Word add-in.
It's also possible to execute the macro from VBA like this (Paraphrased)
Sub AppRun_AddIn_NoArg() Application.Run "ModuleName.MethodName1" End Sub
And with arguments:
Sub AppRun_AddIn_WithArg() Application.Run "ModuleName.MethodName2", "ArgumentValue" End Sub
The "embedded" macros I ask about here, should be executable in the same manner, for maximum compatibility with third parties.