none
Word 2007 - how to add macro to Normal.dotm programmatically RRS feed

  • Question

  • Here is my challenge:
    As part of a customised application installation, I need to insert an autoexec macro into Normal.dotm. I cannot use the startup folder as this is a network shared location and the addins I need to load are licensed and cannot be generally available.
    I have set up a local trusted folder in which the addins are installed, and I have enabled the use of VBA code in the trust centre. The macro I have created works fine when added manually to Normal.dotm, but I can't expect users to go through the process of adding the macro themselves, so I want to do this through code using the Word automation object. Unfortunately I am not a programmer and struggle with the coding of automation scripts. I have exported my macro as a .BAS file, so all that is left is some vbscript code (ideally) that will load the word object and poke the macro code into Normal.dotm.

    I do not want a solution that requires a separate template or which is linked to a pushbutton in the toolbar - this needs to work totally without user involvement.

    Can anyone help me with the code, or point me at a web resource where I can find some code that I can adapt. Needless to say I have spent a long time Googling on various search strings to find an answer, but so far to no avail. I do understand that direct editing of Normal.dotm is not the preferred way of doing this, but in our particular circumstances, that is the only way that maintains compatibility with all the different implementations of Office 2007 at our University.


    EdT

    Saturday, April 6, 2013 4:48 PM

Answers

  • Hi Ed

    I don't have a plug-in solution for you, but two suggestions for possible approaches

    1. Since we're talking Word 2007 that means *.dotm, the Office Open XML file format. It's possible to work with such documents outside the Word application. The tricky part is that the vbaproject in the file is NOT open XML, it's the old binary file format. But the specs on this are now in the public domain, so if you have access to C++ programmers it should theoretically be possible to append your macro to the vbaproject without even running Word. I can give you links to the public information and a forum, but no code.

    2. I'm surprised you haven't come across any discussions / samples using the Visual Basic Extensibility 5.3 library (VBE objectmodel)? That's the interface that lets you work with VBA projects in-process with the Office application (Word). I know there are code samples "out there", but here's a very simple one to get you started:

    NormalTemplate.VBProject.VBComponents( _
      "NewMacros").CodeModule.AddFromString _
      "Sub Test" & vbCr & vbTab & "MsgBox " & _
       Chr(34) & "Hi" & Chr(34) & vbCr & "End Sub"

    AddFromString is one possibility for writing code to a module (VBComponent). It inserts the string at the very beginning, which would be fine for your purposes if there is no Option Explicit or anything else that must be at the top of a module.

    It's also possible to insert a string at any given point (line) in a module. Here's a code snippet that inserts a string at the end of the module:

    totalCodeLines = NormalTemplate.VBProject.VBComponents( _
      "NewMacros").CodeModule.CountOfLines
    NormalTemplate.VBProject.VBComponents( _
      "NewMacros").CodeModule.InsertLines _
        totalCodeLines, _
        "Sub Test" & vbCr & "End Sub"
    
    
    There are other methods and properties that let you determine where certain procedures and parts of procedures are and work based on that information.  

    Cindy Meister, VSTO/Word MVP, my blog

    Monday, April 8, 2013 6:57 AM
    Moderator
  • I'm surprised you haven't come across any discussions / samples using the Visual Basic Extensibility 5.3 library (VBE objectmodel)? That's the interface that lets you work with VBA projects in-process with the Office application (Word).

    Indeed, I've posted such code in this very forum: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/6754e433-41aa-42fd-ad28-07653d8f580b

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, April 8, 2013 9:09 AM

All replies

  • Hi Ed,

    This link might help you Add a Macro to Normal.dotm via VBA

    Also listed below in case the hyperlink doesn't work

    http://social.technet.microsoft.com/Forums/en-US/word/thread/6b5d638a-9ce5-477f-aebb-a03215c6bdb6/


    Kind Regards, Rich ... http://greatcirclelearning.com

    Saturday, April 6, 2013 5:33 PM
  • For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, April 7, 2013 9:01 AM
  • Thanks Rich - that was one of the first hits I read through when researching this problem. The solution posted in that thread has two limitations - 1. It needs to create a toolbar shortcut 2. It requires user intervention.

    I need a solution that bypasses any requirement for user involvement.  A code solution that runs outside Word and does the necessary loading of normal.dotm via the automation object, edits the macro container, and then saves and exits is what I am trying to achieve.

    Thanks

    Ed


    EdT

    Sunday, April 7, 2013 3:38 PM
  • @Paul,

    Thank you for your comments. However, I am aware that my problem is not going to be an easy fix and therefore the wider the audience, the better my chances of getting ONE solution, which is all I need. Either you know how to do it or you don't - I don't believe anyone is going to waste loads of time on my posting if they have no clue on how to answer it, and I note that you have not offered any possible solutions either.

    FYI, I have been doing forum support online for 29 years and have posted over 40,000 times on just one of the forums I support, so I would like to reassure you that I understand about forum etiquette.

    Best wishes

    Ed


    EdT

    Sunday, April 7, 2013 3:44 PM
  • Hi Ed

    I don't have a plug-in solution for you, but two suggestions for possible approaches

    1. Since we're talking Word 2007 that means *.dotm, the Office Open XML file format. It's possible to work with such documents outside the Word application. The tricky part is that the vbaproject in the file is NOT open XML, it's the old binary file format. But the specs on this are now in the public domain, so if you have access to C++ programmers it should theoretically be possible to append your macro to the vbaproject without even running Word. I can give you links to the public information and a forum, but no code.

    2. I'm surprised you haven't come across any discussions / samples using the Visual Basic Extensibility 5.3 library (VBE objectmodel)? That's the interface that lets you work with VBA projects in-process with the Office application (Word). I know there are code samples "out there", but here's a very simple one to get you started:

    NormalTemplate.VBProject.VBComponents( _
      "NewMacros").CodeModule.AddFromString _
      "Sub Test" & vbCr & vbTab & "MsgBox " & _
       Chr(34) & "Hi" & Chr(34) & vbCr & "End Sub"

    AddFromString is one possibility for writing code to a module (VBComponent). It inserts the string at the very beginning, which would be fine for your purposes if there is no Option Explicit or anything else that must be at the top of a module.

    It's also possible to insert a string at any given point (line) in a module. Here's a code snippet that inserts a string at the end of the module:

    totalCodeLines = NormalTemplate.VBProject.VBComponents( _
      "NewMacros").CodeModule.CountOfLines
    NormalTemplate.VBProject.VBComponents( _
      "NewMacros").CodeModule.InsertLines _
        totalCodeLines, _
        "Sub Test" & vbCr & "End Sub"
    
    
    There are other methods and properties that let you determine where certain procedures and parts of procedures are and work based on that information.  

    Cindy Meister, VSTO/Word MVP, my blog

    Monday, April 8, 2013 6:57 AM
    Moderator
  • I'm surprised you haven't come across any discussions / samples using the Visual Basic Extensibility 5.3 library (VBE objectmodel)? That's the interface that lets you work with VBA projects in-process with the Office application (Word).

    Indeed, I've posted such code in this very forum: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/6754e433-41aa-42fd-ad28-07653d8f580b

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, April 8, 2013 9:09 AM
  • Hi EdT,

    I temporarily marked the replies as answers. You can unmark them if they provide no help.

    Please feel free to let us know if you need any further help.

    Have a nice day.


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 17, 2013 12:41 PM
    Moderator