WD2002: FileClose or DocClose Macro Does Not Run When You Close a Document (274008)



The information in this article applies to:

  • Microsoft Word 2002

This article was previously published under Q274008

For a Microsoft Word 2000 version of this article, see 220505.

SYMPTOMS

In Microsoft Word, when you create a macro by using the built-in Word Basic FileClose or DocClose command, the macro may not always run as expected when you close the file or document.

CAUSE

This behavior can occur if you close the file or document by using the Close command (clicking the X) on the Word title bar instead of the Close command on the File menu or the Close Window command (the X) on the document window, as in the following example:
  1. Start Word.
  2. Press ALT+F11 to start the Visual Basic Editor.
  3. On the Insert menu, click Module.
  4. In the module sheet, type the following code:
    Sub DocClose()
    
       WordBasic.DocClose
       MsgBox "DocClose"
    
    End Sub
    
    Sub FileClose()
    
       WordBasic.FileClose
       MsgBox "FileClose"
    
    End Sub
    					
  5. Press ALT+F11 to return to Word.
  6. Open four documents.
  7. On the File menu, click Close.

    A document closes and you receive the following message:

    FileClose

  8. Click the Close Window icon (the X in the upper-right corner of the document window, not the X on the Word title bar).

    A document closes and you receive the following message:

    DocClose

  9. Click the Close icon (the X on the Word title bar).

    A document closes and no message appears.
NOTE: If only one document is open, Word quits.

WORKAROUND

To work around this issue, use the Microsoft Visual Basic for Applications Document Close event.

To create a Document Close event procedure, follow these steps:
  1. Start Word, and then open the document or template that you want.
  2. On the Tools menu, point to Macro, and then click Visual Basic Editor.
  3. On the View menu in Visual Basic Editor, click Project Explorer.
  4. In the Project Explorer window, double-click your document or template project to view Microsoft Word Objects.

    For example, if your document name is "MyDoc," in the Project Explorer window, double-click the following item:

    Project(MyDoc)

  5. In the Project Explorer window, double-click Microsoft Word Objects to view ThisDocument.
  6. In the Project Explorer window, double-click ThisDocument.
  7. In the Object box, (immediately above the code sheet on the left), select Document.
  8. In the Procedure box, (immediately above the code sheet on the right), select Close.
  9. In the code window, type the code that you want between the Private Sub Document_Close and End Sub command lines.

    For example:
    Private Sub Document_Close
    
       msgbox "Finished"
    
    End Sub
    					
  10. On the File menu, click Save filename.
  11. On the File menu, click Close and Return to Microsoft Word.
The macro now runs no matter which method that you use to close the document.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

Modification Type:MinorLast Reviewed:7/27/2006
Keywords:kbbug kbnofix KB274008