操作方法:从 C# Ribbon Addin 运行现有的 Word VBA 宏

发布于 2024-08-11 08:14:23 字数 239 浏览 2 评论 0原文

背景:我有一组广泛的专用 VBA 宏,在 Word 中用于文档格式设置。在Word 2003 中,这些宏是从自定义工具栏激活的。我最近过渡到 Word 2007,希望能够从使用 VS 2010 创建的新 Word 功能区运行这些现有的 VBA 宏。我已经创建了一个功能区;但是,我无法弄清楚如何从新的功能区按钮调用现有的宏。

问题:如何从 C# Word 插件调用存储在 .dotm 模板中的现有 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2024-08-18 08:14:23

MS 知识库文章 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);
}

,然后像这样调用您的宏:

RunMacro(oApp, new object[] {"NameOfMyMacro"})

或者

RunMacro(oApp, new object[] {"NameOfMyMacro", "some", 3, "parameters"})

oApp 是 < code>Word.Application 对象,我确信该对象在 Word 加载项中的某个位置可用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文