PRB: The Open Event Fires More Than One Time When You Open a Managed Code Extension for a Microsoft Office Word 2003 Document (823995)



The information in this article applies to:

  • Microsoft Office Word 2003
  • Microsoft Visual Studio Tools for the Microsoft Office System version 2003



SYMPTOMS

When you open a managed code extension for a Microsoft Office Word 2003 document, the Open event fires more than one time. This behavior might give you the impression that Word has stopped responding.

CAUSE

This problem occurs if all of the following conditions are met: :
  • Your Microsoft Visual Basic project Option Strict setting is set to Off.

    -and-
  • You use one of the following methods in the Open event handler of the document:
    • RecentFile::Add
    • Windows::CompareSideBySideWith
    • XMLNamespace::AttachToDocument
    -and-
  • You pass the ThisDocument variable as a parameter to one of the aforementioned methods.
When you call one of the aforementioned methods, the document reference is passed as a parameter. When the document reference is passed as a parameter to a method that is used in the Open event, the managed code extension calls Word to sink additional objects to its connection point list. This behavior causes the Open event to fire more than one time.

RESOLUTION

To resolve this problem, cast the ThisDocument variable to an Object data type variable, and then pass the Object data type variable. If the Option Strict project setting is turned on, you must cast the document parameter to an Object data type to avoid a compilation error.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET 2003.
  2. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.
  3. In the Project Types list, expand Microsoft Office System Projects, and then click Visual Basic Projects.
  4. In the Templates list, click Word Document, and then click OK.

    The Microsoft Office Project Wizard appears.
  5. In the Microsoft Office Project Wizard, click Finish.
  6. Add the following code to the ThisDocument_Open event handler:
    Dim result As DialogResult = MessageBox.Show( _
        "Continue?", "", MessageBoxButtons.YesNo)
    If result = DialogResult.Yes Then
        ThisApplication.RecentFiles.Add(ThisDocument)
    End If
  7. Press the F5 key to build the solution and to run the solution.

    The document appears in Word. You are prompted to continue.
  8. Click Yes to continue.

    You are prompted to continue again (the Open event fired again).
  9. Click No.
  10. Quit Word.
To correct the problem, replace the code in the ThisDocument_Open event handler with the following code:
Dim result As DialogResult = MessageBox.Show( _
    "Continue?", "", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
    ThisApplication.RecentFiles.Add(CType(ThisDocument, Object))
End If

REFERENCES

For more information, visit the following Microsoft Web site:

Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbprb kbPIA KB823995 kbAudDeveloper