johnywhy
New Member
- Joined
- Sep 12, 2008
- Messages
- 47
- Office Version
- 365
- 2019
- 2016
- 2013
- 2011
- 2010
- 2007
- Platform
- Windows
- MacOS
i'd like to intercept BeforeClose with my own process, and cancel the default process. Not working as expected:
Create a new workbook, enter the following, and save:
ThisWorkbook module:
On the Excel front-end, close the workbook manually. It will close. 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.
Restore EnableEvents.
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? Any ideas?
Create a new workbook, enter the following, and save:
ThisWorkbook module:
VBA Code:
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. It will close. 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.
Restore EnableEvents.
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? Any ideas?