Results 1 to 7 of 7
  1. #1
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    VBA Macro when document opens (Word 2003 SP2)

    I am not sure if this is the correct forum for this. If it isn't then maybe one of the mods can move it. (Thanks)

    I have designed a template for taking and reporting meeting minutes. I want to put a macro in the template such that when the template is opened for a new document, this macro will run and provide a message to the opener of the new document. I do not want this macro to run every time the new document is run, just every time the template is used for a new document. I have spent the better part of an hour looking for something like this - to no avail. I suspect this is something very simple. Any help that anyone would care to provide is greatly appreciated. Thanks. <img src=/S/compute.gif border=0 alt=compute width=40 height=20>

    Ron M <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VBA Macro when document opens (Word 2003 SP2)

    Since the very old days, Word has used the special name AutoNew for a macro which runs when you create a new document from a template.

    Sub AutoNew
    MsgBox "Ding!"
    End Sub

    You also might be able to use an event handler in the ThisDocument module of your template:

    Private Sub Document_New()
    MSgBox "Dong!"
    End Sub

    However, I haven't used that myself, so I'm not sure about its limitations.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA Macro when document opens (Word 2003 SP2)

    Open the template.
    Activate the Visual Basic Editor (Alt+F11)
    Activate the Project Explorer (Ctrl+R)
    Double click ThisDocument.
    Select Document from the dropdown list in the upper left corner of the module window that appears.
    This will create a Document_New event procedure.
    Make it look like this, changing the text as desired. You can add more lines etc.

    Private Sub Document_New()
    MsgBox "This is a test of the emergency" & vbCrLf & _
    "broadcasting system.", vbInformation
    End Sub

    If you need to display info in a more sophisticated way, you could design a userform (Insert | UserForm) and show it from the Document_New procedure.

  4. #4
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: VBA Macro when document opens (Word 2003 SP2)

    Thanks folks. This will be quite useful to me. <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

    Ron M <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  5. #5
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: VBA Macro when document opens (Word 2003 SP2)

    I have a Macro that works as required using the examples suggested in the previous responses, but now it runs everytime I open any template. Is there something I need to do to limit the appearance of the dialog box to the one application/template? Any help or insight is greatly appreciated. Thanks. <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> <img src=/S/compute.gif border=0 alt=compute width=40 height=20>

    Regards,

    Ron M <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VBA Macro when document opens (Word 2003 SP2)

    Move your code from the ThisDocument module in Normal.dot to the ThisDocument module of MyTemplate.dot. Does that resolve it?

  7. #7
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Calgary, Alberta, Canada
    Posts
    818
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: VBA Macro when document opens (Word 2003 SP2)

    This would seem to be the solution. Thanks

    Ron

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •