Share via

How to insert a macro into Normal.dotm programmatically.

Anonymous
2013-04-05T20:34:07+00:00

I have a macro for Word 2007 that I have exported as a .BAS file from the VBA Editor. I now need some code that can use the Word automation object to install this macro into Normal.dotm automatically. I have to use this approach as the Startup Folder is network based and shared in some areas of the University and consequently my addins need to go locally into a nominated trusted folder and then be autoloaded by an autoexec macro whenever Word starts up.

I understand that this approach is not recommended but no other options are possible across the environment I support, and the methodology has been tested by adding the macro code manually. Manual installation is not an option for production though.

Can anyone offer some guidance on how to "open" the Normal.dotm and edit it through ideally a vbscript.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Paul Edstein 82,861 Reputation points Volunteer Moderator
2013-04-05T22:48:55+00:00

You can use code like:

Sub InsertVBComponent(ByVal Doc As Document, ByVal CompFileName As String)

' inserts the contents of CompFileName as a new component in wb

' CompFileName must be a valid VBA component suited for import (an exported VBA component)

If Dir(CompFileName) <> "" Then ' source file exist

 On Error Resume Next ' ignores any errors if the project is protected

 Doc.VBProject.VBComponents.Import CompFileName ' inserts component from file

 On Error GoTo 0

End If

Set Doc = Nothing

End Sub

called with:

InsertVBComponent ActiveDocument, "C:\FolderName\Filename.bas"

Note: To use the above procedures, you need to set a reference to Microsoft Visual Basic For Applications Extensibility and the user running the code must grant access to VBA object model (seehttp://support.microsoft.com/kb/282830).

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-04-16T06:40:17+00:00

    For the benefit of other readers, one of my colleagues in another department was able to volunteer the following code:

    Set myWord = CreateObject("Word.Application")

    myWord.Visible = True  ‘or false for silent installation

    myWord.documents.Add   ‘necessary to open a blank document or else programmatic access to normal.dotm will be denied

    Set myDocument = myWord.normaltemplate

    myDocument.VBProject.VBComponents.Import "C:\MyCode.bas"

    myDocument.Save

    myWord.Quit

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-04-07T16:14:04+00:00

    Thanks - I will check this out when I am back in the office.

    Was this answer helpful?

    0 comments No comments