Run external vba-code in MS Word

╄→гoц情女王★ 提交于 2019-12-10 04:56:56

问题


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.


回答1:


Move the code to the attached template, then the macros are available in all documents based on that template.




回答2:


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().

e.g.

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!"



来源:https://stackoverflow.com/questions/3923778/run-external-vba-code-in-ms-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!