Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run external vba-code in MS Word

Tags:

ms-word

vba

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.

like image 379
Daniel Avatar asked Oct 13 '10 12:10

Daniel


1 Answers

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

like image 188
Steve Ridout Avatar answered Sep 26 '22 05:09

Steve Ridout