i'd like to intercept BeforeClose with my own process, and cancel the default process. Not working as expected. To replicate, create a new workbook, enter the following, and save:
ThisWorkbook module:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = True
Application.EnableEvents = False ' to prevent recursively calling this procedure
ThisWorkbook.Close
End Sub
On the Excel front-end, close the workbook manually by clicking the close-X. It will close, as expected, by the ThisWorkbook.Close statement. In Excel 2016 and 365, if other workbooks are open, they remain open.
Beware, EnableEvents will now be false. Close and reopen Excel to restore it, or enter in immediate pane:
Application.EnableEvents = True
Now reopen the same workbook. Put a breakpoint on Workbook_BeforeClose. Go to front end and again manually close the workbook. Step through the code, to confirm what it's doing. Still works fine.
Restore EnableEvents as described above.
Open the file once more. Go to immediate pane, and enter: ThisWorkbook.Close
The file will not close. Step through the code to see what's happening. Still doesn't close. Why not?


