How-to: Run existing Word VBA Macros from C# Ribbon Addin

非 Y 不嫁゛ 提交于 2019-12-17 19:53:42

问题


Background: I have an extensive set of specialized VBA macros used in Word for document formatting purposes. In Word 2003, these macros were activated from a customized toolbar. I have recently transitioned to Word 2007 and would like to be able to run these existing VBA macros from a new Word Ribbon created with VS 2010. I have created a Ribbon; however, I cannot figure out how to call the existing macros from the new Ribbon buttons.

Question: How do I call the existing VBA macros, which are stored in a .dotm template, from the C# Word Add-in?

Any help would be greatly appreciated.


回答1:


The technique described in MS KB article 306683 -- in particular, function RunMacro defined there -- should allow you to call a VBA macro from within C# code: 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 the Word.Application object, which I'm sure is available somewhere in a Word add-in.



来源:https://stackoverflow.com/questions/1735815/how-to-run-existing-word-vba-macros-from-c-sharp-ribbon-addin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!