Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA; Document to inherit code from template

Status
Not open for further replies.

BrianWen

Programmer
Jun 8, 2009
102
DK
I have a template with a bunch of vba scripts and I'd like that a document made from the template inherits a certain script.

How would I go about that? - There must be some code to 'copy' a module or something like that.

Thanks in advance!
 
Hi Brien,

You can do that by first exporting the code module from the template, then adding the following code to the template's 'This Document' module:
Code:
Private Sub Document_New()
Dim InFile As String
InFile = "C:\Users\Brien Wen\My Documents\Templates\CodeModule.bas"
If Dir(InFile) = "" Then Exit Sub
Application.VBE.ActiveVBProject.VBComponents.Import (InFile)
End Sub
Change the path & filename to match wherever you store the code module. If you need the solution to be portable, you can add code to do the export also (to, say the template's own folder) every time a new file is created and, after importing into the new document, delete the exported module.


Cheers
[MS MVP - Word]
 
Thank you for the answer. I will look into it and get back...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top