Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,604
- Office Version
- 2016
- Platform
- Windows
Hi all.
I have this simple code in ThisWorkbook module that is supposed to hook the Apllication events so that the AppEvents_WorkbookBeforeClose event hadler is fired each time an open workbook is closed.
Now the problem I am having is that if I close a workbook, the BeforeClose event code fires as expected but if I close the main Excel application window, the routine never gets fired.
I know this is due to the background loop routine because when I remove the loop routine everything works as expected.
Can anybody shade some light as to why this is happening and perhaps provide a workaround .
Thanks .
I have this simple code in ThisWorkbook module that is supposed to hook the Apllication events so that the AppEvents_WorkbookBeforeClose event hadler is fired each time an open workbook is closed.
Code:
Option Explicit
Private WithEvents AppEvents As Application
Private Sub Workbook_Open()
Set AppEvents = Application
Call MyLoop
End Sub
Private Sub AppEvents_WorkbookBeforeClose _
(ByVal Wb As Workbook, Cancel As Boolean)
MsgBox "Workbook " & Wb.Name & " closing..."
End Sub
Private Sub MyLoop()
Do
DoEvents
Loop
End Sub
Now the problem I am having is that if I close a workbook, the BeforeClose event code fires as expected but if I close the main Excel application window, the routine never gets fired.
I know this is due to the background loop routine because when I remove the loop routine everything works as expected.
Can anybody shade some light as to why this is happening and perhaps provide a workaround .
Thanks .