none
automatic macro RRS feed

Answers

  • Open the template in Word.

    Press Alt+F11 to activate the Visual Basic Editor.

    Double-click the ThisDocument item under Microsoft Word Objects under the name of the template on the left hand side.

    Create two procedures (subs) in the module that appears:

    Private Sub Document_New()
        ' your code here
        ...
    End Sub
    
    Private Sub Document_Open()
        ' your code here
        ...
    End Sub

    The Document_New procedure will be called each time you create a new document based on the template, and the Document_Open procedure will be called each time you open an existing document based on the template.

    The names Document_New and Document_Open are obligatory, you shouldn't change them. And they *must* be in the ThisDocument module, otherwise they won't work.


    Regards, Hans Vogelaar

    • Marked as answer by hila_d Monday, March 5, 2012 10:28 AM
    Sunday, March 4, 2012 8:59 PM

All replies

  • Do you want to run a macro when you create or open a document based on a specific template (such as the default template Normal.dotm), or whenever you create/open ANY document, regardless of the template it is based on?

    Regards, Hans Vogelaar

    Sunday, March 4, 2012 5:03 PM
  • Take a look at this article on Auto Macros.

    Hopefully it helps


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

    Sunday, March 4, 2012 5:07 PM
  • I want to run a macro when I create or open a document based on a specific template (such as the default template Normal.dotm). how am I do it?   what is the sub name? and var?

    Sunday, March 4, 2012 8:31 PM
  • Open the template in Word.

    Press Alt+F11 to activate the Visual Basic Editor.

    Double-click the ThisDocument item under Microsoft Word Objects under the name of the template on the left hand side.

    Create two procedures (subs) in the module that appears:

    Private Sub Document_New()
        ' your code here
        ...
    End Sub
    
    Private Sub Document_Open()
        ' your code here
        ...
    End Sub

    The Document_New procedure will be called each time you create a new document based on the template, and the Document_Open procedure will be called each time you open an existing document based on the template.

    The names Document_New and Document_Open are obligatory, you shouldn't change them. And they *must* be in the ThisDocument module, otherwise they won't work.


    Regards, Hans Vogelaar

    • Marked as answer by hila_d Monday, March 5, 2012 10:28 AM
    Sunday, March 4, 2012 8:59 PM
  • can I call sub from moudle? how?
    Monday, March 5, 2012 2:10 PM
  • Yes, like this:

    Private Sub Document_New()
        Call Macro1
    End Sub
    
    Private Sub Document_Open()
        Call Macro2
    End Sub

    where Macro1 and Macro2 are macros (Subs) in a standard module.

    Regards, Hans Vogelaar

    Monday, March 5, 2012 2:46 PM