Word VBA events only fire once for documents based on a template
I have a macro enabled template with an event listener that listens for content control exit events. The idea is that when the "title" or "date" content controls on the title page of the document are edited, we automatically update the "title" and "date" content controls in the header so that the author doesn't have to enter the same content twice.
My problem is that, when I open a new document based on my template (right click template => new, or just double click it), these events only fire for the very first instance of a content control being exited. I click inside the CC, click outside the CC, get a MsgBox indicating that my event has fired. I then try that a second time: click inside the CC, click outside the CC and do not get a MsgBox.
Code from my event handler class:
Public WithEvents doc As Word.Document
Private Sub doc_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
MsgBox ContentControl.Range.Text
End Sub
I've checked and found that my event handler object is still defined (not "Nothing") in my NewMacros, it's just that it isn't getting ContentControlOnExit
events, or is ignoring them.
If I change the above code such that I'm not actually doing anything with the content control inside the event body, the problem is fixed - my theory is that touching any sort of content control while inside the ContentControlOnExit
event is triggering recursive ContentControlOnExit
events and somehow causing a problem. Obviously a ContentControlOnExit
event is pretty useless if I'm not allowed to do anything with content controls while inside it.
i.e. receiving a ContentControlOnExit
event doesn't "break" future ContentControlOnExit
events if I change my code to:
Public WithEvents doc As Word.Document
Private Sub doc_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
MsgBox "Content Control exit event"
End Sub
I've tried using an eventsEnabled
boolean to try and guard against doc_ContentControlOnExit
being called recursively in case that's the problem, but it didn't help. The code I used for that was like:
Sub Class_Initialize()
pEventsEnabled = True
End Sub
...
' in the doc_ContentControlOnExit sub:
If pEventsEnabled Then
' obvious race condition...
pEventsEnabled = False
' Fiddle around with some content controls
pEventsEnabled = True
End If
Excel has an Application.EnableEvents
property, but this doesn't seem to be present in Word.
The interesting thing is that this all works fine when editing the template itself, just not for documents based on that template. When editing the template, I get a ContentControlOnExit
event every time I exit a content control, and all of my code works fine.
If it helps, I'm using Word 2010 (Office Professional Plus) on Windows 7 Professional 64 bit. I've also confirmed that the same problem occurs under Word 2007.
edit:
I just tried setting the event handler object to call "ReincarnateEventHandler" in NewMacros, which in turn set the original event handler object to Nothing and then instantiated a new event handler. This resulted in an infinite loop of event handlers handling the first event and then setting up a new event handler which then handled the same (original) event. Using Application.OnTime to delay the reincarnation by 1 sec fixed the infinite loop, bu开发者_如何转开发t didn't fix the original problem — i.e. dropping my first event handler and then instantiating a new event handler doesn't let me catch subsequent events after the first event.
It's a quick fix. Change ThisDocument
to ActiveDocument
in your SetUp
routine. As it is, ThisDocument
refers to the template itself. ActiveDocument
is for the one created from AutoNew
. So it should read Set eventHandler.doc = ActiveDocument
.
It won't interfere with other documents not created with "so_template.dotm" as the ones created from that template will have an .AttachedTemplate
of "so_template.dotm" and the AutoNew
, if present, will control them.
精彩评论