How to create global event procedures similar to AutoOpen, AutoNew and AutoClose, without using Normal.dot

Article contributed by Ibby and Dave Rado

1.

Using Word 97

If you create a DocumentChange event and store it in an Addin, it will be triggered when any document is created, opened, closed or the focus changes from one document to another. You can take advantage of this to simulate AutoNew, AutoOpen, AutoClose macros (as well as being able to monitor when the focus changes). Unlike the application event DocumentChange, AutoNew, AutoOpen and AutoClose macros only behave globally if stored in Normal.dot.

You can do it as follows:

In a Module:

Option Explicit

Dim oAppClass As New ThisApplication
Public oldNoOfOpenDocs As Long
Public FirstNewDoc As Boolean

Public Sub AutoExec()
    Set oAppClass.oApp = Word.Application
    oldNoOfOpenDocs = 0
    FirstNewDoc = True
End Sub

In a Class Module (name this ThisApplication):

Option Explicit

Public WithEvents
oApp As Word.Application


Private Sub oApp_DocumentChange()

On Error GoTo ExitCode

Dim newNoOfOpenDocs As Long
Dim docAdded As Boolean
Dim docClosed As Boolean

newNoOfOpenDocs = Application.Documents.Count

If newNoOfOpenDocs > oldNoOfOpenDocs Then
    docAdded = True

    If ActiveDocument.Name = "Document1" And FirstNewDoc Then
        FirstNewDoc = True
    Else
        FirstNewDoc = False
    End If
    oldNoOfOpenDocs = oldNoOfOpenDocs + 1

ElseIf
oldNoOfOpenDocs > newNoOfOpenDocs Then
    docClosed = True
    FirstNewDoc = False
    oldNoOfOpenDocs = oldNoOfOpenDocs - 1
End If

If docAdded Then
    If Len(ActiveDocument.Path) = 0 Then
    Call PsuedoAutoNew
Else
    Call PsuedoAutoOpen
End If

ElseIf docClosed Then
    Call PsuedoAutoClose

ElseIf FirstNewDoc Then
    If Len(ActiveDocument.Path) = 0 Then
        Call PsuedoAutoNew
    Else
        Call PsuedoAutoOpen
    End If

Else
    Call DocChangedFocus
End If

Exit Sub

ExitCode:

End Sub


Private Sub PsuedoAutoNew()
     'Your code here
End Sub


Private Sub PsuedoAutoOpen()
     'Your code here
End Sub


Private Sub PsuedoAutoClose()
    'Your code here
End Sub


Private Sub DocChangedFocus()
    'Your code here
End Sub

Notes:

1.

Unfortunately, unlike a true AutoClose macro or DocumentBeforeClose event, the PsuedoAutoClose fires after the document has closed rather than before; so in this respect, this workaround is no substitute for the real thing; but maybe it's better than no workaround at all. (This is logical, if unfortunate, because the document focus hasn't changed until the document being closed has completely closed.).

Also, (and unfortunately), if you quit Word with only one document open, the PsuedoAutoClose macro doesn't fire at all.

And if you quit Word using File + Exit, even the (Word 2000-specific) DocumentBeforeClose event, which is covered below, fires after the message asking whether you want to save changes has appeared!! This bug is unfortunate to say the least, as it means that if you want to perform some validation before that dialog appears, you have to use an AutoClose macro (see the article: A pseudo DocumentBeforeClose Event).

2.

In one important respect, the PsuedoAutoNew and PsuedoAutoOpen macros work better than the Word 2000-specific NewDocument and DocumentOpen events.

When you start Word, a new blank document is created; the PsuedoAutoNew macro runs, but neither AutoNew nor the (Word 2000-specific) NewDocument events are triggered.

Similarly, if you start Word with an AutoExec macro that opens a document, or that allows the user to open a document, for example:

Public Sub AutoExec()

Dim myFile As String
myFile = Application.RecentFiles(1).Path & "\" _
  & Application.RecentFiles(1).Name
Documents.Open myFile

End Sub

... AutoOpen and the PsuedoAutoOpen macro both fire, but the (Word 2000-specific) DocumentOpen event is not triggered.

So even Word 2000 developers who don't have to cater for Word 97 users may still prefer to use the use the DocumentChange event to simulate AutoOpen and AutoNew, as it's more reliable.

2.

Using Word 2000 and later versions

In Word 2000 and above, you can store NewDocument, DocumentOpen and DocumentBeforeClose event procedures in an Addin, and they will be global. They would look like this:

Private Sub oApp_NewDocument(ByVal Doc As Document)
    'Your code here
End Sub

Private Sub oApp_DocumentOpen(ByVal Doc As Document)
    'Your code here
End Sub

Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, _
 Cancel As Boolean)
    'Your code here
End Sub

In the case of the DocumentBeforeClose event, the Cancel parameter allows you to prevent the document closing if required, e.g.:

Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)

If ActiveDocument.Paragraphs.Count > 3 Then
    Cancel = True
    MsgBox "You are not allowed more than 3 paragraphs."
End If

End Sub

But see the Notes at the end of the Word 97 section for a discussion of the numerous gotchas associated with this – and with all three of these events – and for some workarounds.