none
Word 2016, New Document Event does not fire when I start Word RRS feed

  • Question

  • I have Word 2016, Windows 10, and want a user form to show when I start Word.
    But only if it is a new document.
    It works if I choose New from an open document. Then the Document_New event fires.
    But when I open Word I think I get a new document, but the Document_New event does not fire. Same thing with Document_Open event.
    If I put frmInfo.Show in an AutoExec procedure it open always. But I only want it to open for new documents.

    Microsoft recommends following (in Normal.dotm):

    1) Create a new class module containing following code:
       Public WithEvents App As Word.Application
    2) Choose an event for App from the event list in the class module:
       Private Sub App_DocumentChange()

       End Sub
           (I choose Private Sub App_NewDocument(ByVal Doc As Document))
    3) Connect the declared object in the class module (App in this example) with the Application object. It can be done from any module:
       Dim X As New EventClassModule
       Sub Register_Event_Handler()
           Set X.App = Word.Application
       End Sub
    4) Run the Register_Event_Handler procedure. After the procedure is run, the App object in the class module points to the Microsoft Word Application object, and the event procedures in the class module will run when the events occur.

    I have done all this, but I cannot get it to work.
    Here is my code:

    Class module
    ===========
    Option Explicit
    'Declare the Object Variable
    Public WithEvents App As Word.Application

    'Write the Event Procedures
    Private Sub App_DocumentOpen(ByVal Doc As Document)
        If App.NewDocument = True Then
            g_strNewDocument = Doc.Windows(1).Caption
            frmInfo.Show
        Else
            MsgBox "No show via Open"
        End If
    End Sub

    Private Sub App_NewDocument(ByVal Doc As Document)
        If App.NewDocument = True Then
            frmInfo.Show
        Else
            MsgBox "No show via New"
        End If
    End Sub

    Public module
    ============
    Option Explicit
    Dim X As New clsAppEvents

    'Initialize the Declared Object
    Public Sub Register_Event_Handler()
        Set X.App = Word.Application
    End Sub

    I also read that events in a document created from Normal.dotm hade to have a reference to Normal.dotm.
    So I added following to ThisDocument:

    Option Explicit

    Public Sub AutoExec()
        Dim tpl As Template
        On Error Resume Next
        Set tpl = ThisDocument.AttachedTemplate
        tpl.Saved = True
        Register_Event_Handler
        MsgBox "AutoExec"
    End Sub

    Private Sub Document_New()
        frmInfo.Show
        MsgBox "New"
    End Sub

    Private Sub Document_Open()
        MsgBox "Open"
    End Sub

    I appreciate any idea!

    Hans Hansson

    Tuesday, September 12, 2017 9:45 AM

Answers

  • Hi toyou,

    I try to test the issue and I get same output as you.

    when start screen is off and you create new document using exe file then the new document event not get fired.

    I try to find the details and find that it will only execute if document is created based on any template.

    then I try to refer AutoNew() macro.

    Another way of achieving the same objective is to create a Module (Insert + Module), and write a macro called AutoNew(), AutoOpen(), or AutoClose(). If stored in any template other than Normal.dot, these will behave in the same way as Document events; i.e. they will be fired when documents attached to the template are created, opened or closed. 

    However, if stored in Normal.dot, they will act globally – in other words, they will be fired when any document is created, opened, or closed. This is in contrast with a Document_Open procedure stored in Normal.dot, which will only execute when documents based on Normal.dot are opened.

    but it also have the same issue.

    so here I think that this is the limitation of Word.

    Reference:

    Running a macro automatically when a document is created, opened or closed

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by toyou Wednesday, September 13, 2017 9:35 AM
    Wednesday, September 13, 2017 8:24 AM
    Moderator

All replies

  • Hi toyou,

    I try to open VBE Editor from Word Application by pressing Alt + F11 key.

    then I try to expand Microsoft Word objects folder.

    I open This Document module and add code below in it.

    Private Sub Document_New()
    MsgBox ("new document created")
    End Sub
    
    Private Sub Document_Close()
    MsgBox ("document closed")
    End Sub

    I save it and close it.

    now when ever I create a new document, it shows me a message that I wrote in new document event.

    and it shows me message every time I close document.

    you can see my testing below.

    you can see that it only displays the message when I create new document but it don't show any message when I open existing document.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, September 13, 2017 2:12 AM
    Moderator
  • Hi Deepak,

    Thank you for your answer.

    Yes, I get that too. That's not the problem. The problem is that I want this to work without going via the New Template window. I have turned that off. When I start Word I want to open a Word document from normal.dotm at once and I want the Document_New event to fire off.
    AutoExec works, but I cannot get Word to tell me that it is a new document.

    Regards,

    Hans


    Hans Hansson

    Wednesday, September 13, 2017 6:14 AM
  • Hi toyou,

    I try to test the issue and I get same output as you.

    when start screen is off and you create new document using exe file then the new document event not get fired.

    I try to find the details and find that it will only execute if document is created based on any template.

    then I try to refer AutoNew() macro.

    Another way of achieving the same objective is to create a Module (Insert + Module), and write a macro called AutoNew(), AutoOpen(), or AutoClose(). If stored in any template other than Normal.dot, these will behave in the same way as Document events; i.e. they will be fired when documents attached to the template are created, opened or closed. 

    However, if stored in Normal.dot, they will act globally – in other words, they will be fired when any document is created, opened, or closed. This is in contrast with a Document_Open procedure stored in Normal.dot, which will only execute when documents based on Normal.dot are opened.

    but it also have the same issue.

    so here I think that this is the limitation of Word.

    Reference:

    Running a macro automatically when a document is created, opened or closed

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by toyou Wednesday, September 13, 2017 9:35 AM
    Wednesday, September 13, 2017 8:24 AM
    Moderator
  • Thank you Deepak,

    OK. I will have to try another way then.

    I also tried to activate PERSONAL templates in Word startup window as default, but I could not find out how. I am trying to direct the users to only see the Companys tailored templates.
    Is that also outside the possibilities?

    Regards,

    Hans


    Hans Hansson

    Wednesday, September 13, 2017 8:35 AM
  • Hi toyou,

    you had mentioned that,"I also tried to activate PERSONAL templates in Word startup window as default, but I could not find out how."

    you can try to use the way mentioned below may work for you.

    (1) Create a shortcut for Word.
    • (2) Right-click the shortcut, and then click Properties.
    • (3) Click the Shortcut tab, and then change the Target line to the following line:
      C:\Word Folder\winword.exe /ttemplate name
      In this line, Word Folder is the path to the Winword.exe program, and template name is the full path to the template.

      If the path or the template file name uses a long file name, enclose the path and template name in quotation marks. Make sure there is no space between the /t switch and template name.

    Reference:

    How to start Word with a template other than the Normal template in Word

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 13, 2017 9:27 AM
    Moderator
  • Hi Deepak,

    Yes, that is one way, but it is several templates in the folder. To many to create shortcuts to all. And the templates will change during time.

    But thank you for your answers.
    I now know more of Words limits ;-)

    Regards,

    Hans


    Hans Hansson

    Wednesday, September 13, 2017 9:35 AM