操作方法:从 C# Ribbon Addin 运行现有的 Word VBA 宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1735815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How-to: Run existing Word VBA Macros from C# Ribbon Addin
提问by BlueDevil
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.
背景:我在 Word 中使用了大量专门的 VBA 宏,用于文档格式设置。在 Word 2003 中,这些宏是从自定义工具栏激活的。我最近过渡到 Word 2007,希望能够从使用 VS 2010 创建的新 Word Ribbon 运行这些现有的 VBA 宏。但是,我无法弄清楚如何从新的功能区按钮调用现有的宏。
Question: How do I call the existing VBA macros, which are stored in a .dotm template, from the C# Word Add-in?
问题:如何从 C# Word 插件调用存储在 .dotm 模板中的现有 VBA 宏?
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by 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
MS KB 文章 306683 中描述的技术——特别是在RunMacro
那里定义的函数——应该允许您从 C# 代码中调用 VBA 宏:您定义一个函数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.
oApp
是Word.Application
对象,我确信它在 Word 加载项中的某处可用。