Word中运行外部vba代码

Word中运行外部vba代码

本文介绍了在MS Word中运行外部vba代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将外部代码链接到Word文档吗?我有很多带有宏(VBA代码)的Word文档.全部具有相同的代码.我希望代码从外部源运行,而不是从所有这些文档中运行.这样,如果我必须更新代码,则只有一个地方需要更新.

Can I link in external code to a Word document?I have a lot of word documents with macros (VBA-code). All with the same code. I would like the code to be run from an external source instead of from within all of those documents. That way, if I have to update the code, I only have one place where I have to do the update.

推荐答案

您可以创建一个模板并将其放在%APPDATA%\ Microsoft \ Word \ STARTUP文件夹中,这使该模板成为插件,而STARTUP是受信任的位置与在其他位置的模板中使用宏相比,这将给您带来更少的安全性问题.

You can create a template and put it in the %APPDATA%\Microsoft\Word\STARTUP folder, this makes the template an addin and STARTUP is a trusted location which will give you fewer security issues than using macros in templates from other locations.

然后,任何文档都可以使用Application.Run()来调用模板中的函数.

Then, any document can call a function in the template using Application.Run().

例如

在您的模板中输入以下内容:

In your template write following:

Function templateHello() As String
    templateHello = "hello from template!"
End Function

然后,您可以在任何Word文档中编写:

Then, in any Word document you can write:

MsgBox Application.Run("templateHello")

这将显示一个消息框,其中包含模板中的hello!"

Which will display a message box with "hello from template!"

这篇关于在MS Word中运行外部vba代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:25