简体   繁体   English

如何在另一个 MS Word 文档中调用 VBA 宏?

[英]How to call a VBA-macro in another MS Word document?

in MS Excel I have been distributing a xls-file that contains a macro.在 MS Excel 中,我一直在分发一个包含宏的 xls 文件。 From the users "random" current open workbook I have successfully been calling this macro by telling Word in which file to look for it.从用户“随机”当前打开的工作簿中,我通过告诉 Word 在哪个文件中查找它,成功地调用了这个宏。 Like this, when the user clicks a button on the ribbon:像这样,当用户单击功能区上的按钮时:

C:\\\\"'MyExcelFileWithTheMacro.xls'!MyMacroName" C:\\\\"'MyExcelFileWithTheMacro.xls'!MyMacroName"

Can I do the same in MS Word?我可以在 MS Word 中做同样的事情吗? That is, to call a macro in a Word-file located in any given folder from an instance of Word that has not opened that file?也就是说,要从尚未打开该文件的 Word 实例中调用位于任何给定文件夹中的 Word 文件中的宏?

I have tried, of course, but Word keeps saying it can't find the macro.我当然试过了,但是 Word 一直说找不到宏。

The reason I want to do it this way is that it is makes for easy distribution and updating of the macro.我想这样做的原因是它可以轻松分发和更新宏。 Next up is signing the macro.接下来是签署宏。 I hope it can be done when doing things this way, but not sure.我希望这样做时可以这样做,但不确定。

No, it's not possible to call and run a macro in any Office application if the file that contains the macro is not loaded in the application interface.否,如果应用程序界面中未加载包含宏的文件,则无法在任何Office 应用程序中调用和运行宏。

That being said, if you place the macro in a *.dotm template and have the user put the template in the STARTUP folder used by the Word application, then Word will load the template as an "Add-in".也就是说,如果您将宏放在 *.dotm 模板中,并让用户将该模板放在 Word 应用程序使用的 STARTUP 文件夹中,则 Word 会将模板作为“加载项”加载。 And in that case you should be able to access the macro.在这种情况下,您应该能够访问宏。

Or, if you don't want it in the Startup folder and you have code that automates the Word.Application, anyway, then that code can load the *.dotm as an "Addin", which should make the macro available (and remove it when you're done with it).或者,如果你不希望它在启动文件夹,你必须能够自动一个Word.Application代码,反正那么该代码可以加载* .dotm为“加载项”,这将使宏可用的(并删除当你完成它时)。 Research Application.Addins.Load and Addin.Installed in the Word language reference as well as on-line for examples.在 Word 语言参考以及在线示例中研究Application.Addins.LoadAddin.Installed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM