简体   繁体   中英

Run external vba-code in MS 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.

将代码移动到附加的模板 ,然后基于该模板的所有文档中都可以使用宏。

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.

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

eg

In your template write following:

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

Then, in any Word document you can write:

MsgBox Application.Run("templateHello")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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