2004.07.28 04:01 PM

Old Word Bugs New Again

Recently ran into an old Word bug I discovered and reported to Microsoft back in November 2002. I originally discovered it in Word 2000, and later reproduced it in Word 2002. It was not a problem in Word 97. I can now confirm that it continues to lurk in Word 2003. And, based on testing I've just done, it appears to be worse.

Originally, the bug was that certain actions performed on a new document during execution of the AutoNew routine (or Document.New event) in the new document's template caused Word to raise multiple Application.DocumentChange events. This proved particularly problematic for me, because the application I was working on used a global add-in to monitor document change events in order to perform document-specific post-creation/open activities, like toolbar and keymapping manipulations.

In order to trigger this behavior in Word 2000 and 2002, you had to do something to the new document from within its template's AutoRun routine. For instance, add a picture to the header:

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes.AddPicture _
    ThisDocument.Path & "\sample.bmp", False, True

Or, add some text to its body (or header):

ActiveDocument.Range.Text = "This is a new document."

However, what I just noticed while testing in Word 2003 is that it's no longer necessary to do something to the new document to get this behavior. Simply creating a new document based on a template, even one with no code in it, is sufficient to trigger at least two Application.DocumentChange events.

If you want to try this yourself, open Word 2003 and create a new document. Go to the VBA editor and insert a new class module into the document's VBA project. Name it WordAppSink and paste the following code into it:

Option Explicit

Private WithEvents oWordApp As Word.Application

Private Sub oWordApp_DocumentChange()
  If Application.Documents.Count = 0 Then
    Call MsgBox("ActiveDocument.Name=(no documents open)", vbInformation, "DocumentChange Event")
  Else
    Call MsgBox("ActiveDocument.Name=" & ActiveDocument.Name, vbInformation, "DocumentChange Event")
  End If
End Sub

Private Sub Class_Initialize()
  Set oWordApp = Word.Application
End Sub

Private Sub Class_Terminate()
  Set oWordApp = Nothing
End Sub

Now insert a new module and paste the following code into it:

Option Explicit

Private m_oWordAppSink As WordAppSink

Sub AutoNew()
  Call MsgBox("Not for new documents.", vbExclamation, ThisDocument.Name)
  Call ActiveDocument.Close(False)
End Sub

Sub Autoexec()
  Set m_oWordAppSink = New WordAppSink
End Sub

Sub Autoexit()
  Set m_oWordAppSink = Nothing
End Sub

Save the document as a DOT file in your default templates directory and close it. Create a new blank document, type some text into it, save it as a DOT file in your default templates directory, and close it. Finally, open the Templates and Add-ins dialog via the Tools menu, click the Add button and select the first DOT file you saved.

At this point you have a global add-in loaded that will alert you whenever a Word document is created, opened, closed, or given focus. Try it out by clicking the new document icon on the toolbar. This creates a new blank document bound to Normal.dot. You should see a single message box that says something like "ActiveDocument.Name=documentX", where the "X" varies depending on how many documents you've created in this session.

Now create a new document using the File | New menu. (Make sure you first close the document you just created, and any other documents you may have opened. I've noticed different behavior depending on whether documents are open and the order of execution. In fact, you may need to do this step twice.) This will show you Word's New Document panel. Select the "On my computer..." link beneath Templates. Use the resulting dialog to select the second DOT file you saved above. This will create a new document based on the simple template you created. And, here's the bug, you should get two message boxes (one after the other) with the same ActiveDocument.Name.

After much back and forth with Microsoft on this issue, they eventually agreed it was a bug and left me with this response, "If it is deemed a fixable issue, it may be changed in the next service patch or the next version of Word."

Apparently, it wasn't fixable.


Comments

I'm having the same problem, and nothing can be found on internet about this

Giovanni | 2005.02.17 05:54 AM


TrackBack

TrackBack URL:  http://www.typepad.com/services/trackback/6a00d8341c7bd453ef00d8346c601669e2

Listed below are links to weblogs that reference Old Word Bugs New Again: