Microsoft KB Archive/274008

= WD2002: FileClose or DocClose Macro Does Not Run When You Close a Document =

Article ID: 274008

Article Last Modified on 7/27/2006

-

APPLIES TO


 * Microsoft Word 2002 Standard Edition

-



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:  Start Word. Press ALT+F11 to start the Visual Basic Editor. On the Insert menu, click Module.  In the module sheet, type the following code: Sub DocClose

WordBasic.DocClose MsgBox &quot;DocClose&quot;

End Sub

Sub FileClose

WordBasic.FileClose MsgBox &quot;FileClose&quot;

End Sub  Press ALT+F11 to return to Word. Open four documents.</li> On the File menu, click Close.

A document closes and you receive the following message:

FileClose

</li> 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

</li> Click the Close icon (the X on the Word title bar).

A document closes and no message appears.</li></ol>

NOTE: If only one document is open, Word quits.

<div class="workaround_section">

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:

<ol> Start Word, and then open the document or template that you want.</li> On the Tools menu, point to Macro, and then click Visual Basic Editor.</li> On the View menu in Visual Basic Editor, click Project Explorer.</li> In the Project Explorer window, double-click your document or template project to view Microsoft Word Objects.

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

Project(MyDoc)

</li> In the Project Explorer window, double-click Microsoft Word Objects to view ThisDocument.</li> In the Project Explorer window, double-click ThisDocument.</li> In the Object box, (immediately above the code sheet on the left), select Document.</li> In the Procedure box, (immediately above the code sheet on the right), select Close.</li>  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 &quot;Finished&quot;

End Sub </li> On the File menu, click Save .</li> On the File menu, click Close and Return to Microsoft Word.</li></ol>

The macro now runs no matter which method that you use to close the document.

<div class="status_section">

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

Additional query words: WD2002 prb

Keywords: kbbug kbnofix KB274008

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.