问题
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