如何:从C#功能区加载项运行现有的Word VBA宏 [英] How-to: Run existing Word VBA Macros from C# Ribbon Addin
问题描述
背景:我有一套广泛的Word专用VBA宏,用于文档格式化。在Word 2003中,这些宏是从自定义工具栏激活的。我最近过渡到Word 2007,希望能够从VS 2010创建的新Word功能区中运行这些现有的VBA宏。但是,我无法弄清楚如何从新的功能区按钮调用现有的宏。
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.
问题:如何调用存储在.NET中的现有VBA宏。 dotm模板,来自C#Word加载项?
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.
推荐答案
MS KB文章306683 -特别是在那里定义的函数 RunMacro
-应该允许您从C#代码中调用VBA宏:您定义一个函数 RunMacro
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"})
或
RunMacro(oApp, new object[] {"NameOfMyMacro", "some", 3, "parameters"})
oApp
是 Word.Application
对象,我确定它在Word加载项的某个位置可用。
oApp
is the Word.Application
object, which I'm sure is available somewhere in a Word add-in.
这篇关于如何:从C#功能区加载项运行现有的Word VBA宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!