Event DocumentChange fires too often

R

Remo

We use the event 'DocumentChange' to set the status of some symbols in
our symbol-bar. We found out that the event is fired every time when a
field (eg. Author-Field) or a table is inserted in the footer. That
means: If we enter 10 fileds in the footer, DocumentChange is fired 10
times. I think this is wrong. In this case the event should never be
fired.

Has anyone an idea how to prevent DocumentChange from beeing fired so
often? Is there a better Event to be used to set the status of symbols
enery time when the user changes to an other document, creates a new
document an so on?
Here is the code in the classmodule we use:

Public WithEvents App As Word.Application
'Handling the event "DocumentChange"
Private Sub App_DocumentChange()
Debug.Print "DocumentChange"
End Sub
'Handling the event "NewDocument"
'
'Every time a new document is created, we want to build a new footer
in this document.
'
Private Sub App_NewDocument(ByVal Doc As Document)
Debug.Print "NewDocument"
Dim Fusszeile As Range
Set Fusszeile = ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range

Debug.Print "Insert Feld"
ActiveDocument.Fields.Add Fusszeile, wdFieldCreateDate
Debug.Print "Insert Feld"
ActiveDocument.Fields.Add Fusszeile, wdFieldCreateDate
Debug.Print "Feld einfügen"
ActiveDocument.Fields.Add Fusszeile, wdFieldCreateDate
Debug.Print "Insert Feld"
End Sub
 
P

Pete Bennett

Word tends to fire off DocumentChange events for lots of different reasons.

What I did to prevent this (so in effect there's one document change event
for each time you either open or change to another document) is to declare a
string variable in the change class and use it to keep track of what the
current document is.

This variable is initialised with the name of the current document if it's
blank.

If the variable has data in, then it's compared to the current document (in
the DocumentChange function itself) and if its the same then there's an Exit
Sub (to stop all your custom change code from happening.

Something like

If len (sCurrName) > 0 then
If strcomp(sCurrentName, ActiveDocument.FullName, vbTextCompare) = 0 then
sCurrentName=ActiveDocument.FullName
Exit Sub ' same doc name so go no further
EndIf
Else
sCurrentName=ActiveDocument.FullName
End If

(rest of event code here)

This means that the code will only trigger when the application changes
between documents (and not when the document itself changes).

Hope that helps.

Pete.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top