XL: Cannot Use Workbook_BeforeClose Event to Close a Workbook (816138)



The information in this article applies to:

  • Microsoft Excel 2002
  • Microsoft Excel 2000
  • Microsoft Excel 97 for Windows

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.

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.

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.

MORE INFORMATION

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

Modification Type:MajorLast Reviewed:5/28/2003
Keywords:kbnofix kbBug KB816138