Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

You cannot use the Workbook_BeforeClose event to close another Excel workbook


View products that this article applies to.

Symptoms

When you programmatically open or close an Excel workbook, you may find that you cannot use the Workbook_BeforeClose event to close another Excel workbook.

For example, when you create and save a macro in Book2.xls that uses ThisWorkbook.Close to close Book2.xls and also uses the Workbook_BeforeClose event to close Book1.xls, the Book1.xls file is not closed as expected when you run the macro.

↑ Back to the top


Cause

This occurs because the Saved property of the workbook is set as False after the workbook is saved. As a result, when you programmatically close a workbook, you cannot use the Workbook_BeforeClose event to close another workbook.

↑ Back to the top


Workaround

To work around this issue, close the workbook in the primary set of macros and do not use the Workbook_BeforeClose to close the workbook from another workbook.

↑ Back to the top


More information

For more information about the BeforeClose event, visit the following Microsoft Web site:

↑ Back to the top


Keywords: KB816138, kbbug, kbnofix

↑ Back to the top

Article Info
Article ID : 816138
Revision : 6
Created on : 2/1/2007
Published on : 2/1/2007
Exists online : False
Views : 364