Link to home
Start Free TrialLog in
Avatar of barges
bargesFlag for Sweden

asked on

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

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 put frmInfo.Show in an AutoExec procedure it open always.

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

Open in new window

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

Open in new window

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

Open in new window


I appreciate any idea!
Avatar of barges
barges
Flag of Sweden image

ASKER

Thank you Rgonzo1971,
I have changed my code as you suggest, but nothing happens when i open Word.

The class module is named clsAppEvents and all code is in Normal.dotm.
Regards,
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.