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

谁说我不能喝 提交于 2019-11-28 11:39:35
Heinzi

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.

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