M
Mick Hardy
This is for general knowledge as I've spent a few days cursing and may
save someone else some time. I'll test my template on Word 2000
tonight but for the moment, these features are particular to Word 2002
SP2.
According to MSDN, the DocumentChange event is supposed to fire "when
a new document is created, when an existing document is opened, or
when another document is made the active document." This is true.
However, this event also fires when you update fields in a header and
when you switch between applications with no documents open. These
are the two I got bitten by. I dread to think how many more are
lurking.
If this is a documented KB article, I apologise, but I couldn't find
one.
To see this for yourself and confirm my suspicians, add the following
code to a module in a template. The code is hacky and uncommented. I
make no apologies.
Private mclsDC As New Class1
Public Sub AutoNew()
Set mclsDC.App = Word.Application
End Sub
Public Sub AutoExec()
Set mclsDC.App = Word.Application
End Sub
Public Sub AutoClose()
Set mclsDC = Nothing
End Sub
Public Sub TriggerDocChange()
Dim hdr As HeaderFooter
Dim sec As Section
For Each sec In ActiveDocument.Sections
For Each hdr In sec.Headers
hdr.Range.Fields.Update
Next
Next
End Sub
Add the following code to a new class module with default name
Private WithEvents cwrdApp As Word.Application
Public Property Get App() As Word.Application
Set App = cwrdApp
End Property
Public Property Set App(ByVal vwrdNewValue As Word.Application)
Set cwrdApp = vwrdNewValue
Set vwrdNewValue = Nothing
End Property
Private Sub Class_Terminate()
Set cwrdApp = Nothing
End Sub
Private Sub cwrdApp_DocumentChange()
Dim rng As Range
If Documents.Count > 0 Then
Set rng = ActiveDocument.Content
rng.Collapse wdCollapseEnd
rng.Text = "Change doc, " _
& ActiveDocument.Name & " at " & CStr(Now) & vbCr
rng.Collapse wdCollapseEnd
Else
MsgBox "Change doc, No docs"
End If
End Sub
Create a new document based on the above template.
Create three pages, set different first page and different odd even
pages in the headers and footers. Add a simple time field to the
three headers. Run the TriggerDocChange macro and you will see the
document change event trigger three times as recorded at the end of
the document. Add ten continuous sections to the document and the
DocumentChange event will trigger 33 times. Nice one Microsoft!
Close your new document and add the template using the Templates and
Addins menu item. Close all documents and you should get a message
box. Switch to another application and then back to Word and you'll
get the message box again. This may be normal behaviour but the first
issue is a bug!
save someone else some time. I'll test my template on Word 2000
tonight but for the moment, these features are particular to Word 2002
SP2.
According to MSDN, the DocumentChange event is supposed to fire "when
a new document is created, when an existing document is opened, or
when another document is made the active document." This is true.
However, this event also fires when you update fields in a header and
when you switch between applications with no documents open. These
are the two I got bitten by. I dread to think how many more are
lurking.
If this is a documented KB article, I apologise, but I couldn't find
one.
To see this for yourself and confirm my suspicians, add the following
code to a module in a template. The code is hacky and uncommented. I
make no apologies.
Private mclsDC As New Class1
Public Sub AutoNew()
Set mclsDC.App = Word.Application
End Sub
Public Sub AutoExec()
Set mclsDC.App = Word.Application
End Sub
Public Sub AutoClose()
Set mclsDC = Nothing
End Sub
Public Sub TriggerDocChange()
Dim hdr As HeaderFooter
Dim sec As Section
For Each sec In ActiveDocument.Sections
For Each hdr In sec.Headers
hdr.Range.Fields.Update
Next
Next
End Sub
Add the following code to a new class module with default name
Private WithEvents cwrdApp As Word.Application
Public Property Get App() As Word.Application
Set App = cwrdApp
End Property
Public Property Set App(ByVal vwrdNewValue As Word.Application)
Set cwrdApp = vwrdNewValue
Set vwrdNewValue = Nothing
End Property
Private Sub Class_Terminate()
Set cwrdApp = Nothing
End Sub
Private Sub cwrdApp_DocumentChange()
Dim rng As Range
If Documents.Count > 0 Then
Set rng = ActiveDocument.Content
rng.Collapse wdCollapseEnd
rng.Text = "Change doc, " _
& ActiveDocument.Name & " at " & CStr(Now) & vbCr
rng.Collapse wdCollapseEnd
Else
MsgBox "Change doc, No docs"
End If
End Sub
Create a new document based on the above template.
Create three pages, set different first page and different odd even
pages in the headers and footers. Add a simple time field to the
three headers. Run the TriggerDocChange macro and you will see the
document change event trigger three times as recorded at the end of
the document. Add ten continuous sections to the document and the
DocumentChange event will trigger 33 times. Nice one Microsoft!
Close your new document and add the template using the Templates and
Addins menu item. Close all documents and you should get a message
box. Switch to another application and then back to Word and you'll
get the message box again. This may be normal behaviour but the first
issue is a bug!