Option Explicit
Private WithEvents Cmbrs_Events As CommandBars
#If VBA7 Then
Private Declare PtrSafe Function GetActiveWindow Lib "user32" () As Long
Private Declare PtrSafe Function GetAncestor Lib "user32" (ByVal hUf As Long, ByVal gaFlags As Long) As Long
Private Declare PtrSafe Function AnyPopup Lib "user32" () As Long
#Else
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetAncestor Lib "user32" (ByVal hUf As Long, ByVal gaFlags As Long) As Long
Private Declare Function AnyPopup Lib "user32" () As Long
#End If
Private Sub Workbook_Activate()
Call MonitorActivateEvent(True)
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Call MonitorActivateEvent(True)
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call MonitorActivateEvent(False)
End Sub
Private Sub MonitorActivateEvent(ByVal bMonitor As Boolean)
If bMonitor Then
If Cmbrs_Events Is Nothing Then
Set Cmbrs_Events = Application.CommandBars
End If
Else
Set Cmbrs_Events = Nothing
End If
End Sub
Private Sub Cmbrs_Events_OnUpdate()
Const GA_ROOT = 2&
Static bNoPopUps As Boolean
Static hPrevHnwd As Long
On Error Resume Next
If hPrevHnwd = 0& Then
If GetAncestor(GetActiveWindow, GA_ROOT) = Application.hwnd And bNoPopUps Then
Application.OnTime Now, Me.CodeName & ".Excel_Pseudo_Activate_Event"
End If
End If
hPrevHnwd = GetActiveWindow: bNoPopUps = (AnyPopup = 0&)
End Sub
' PSEUDO-EVENT
Private Sub Excel_Pseudo_Activate_Event()
MsgBox "Excel Activated."
End Sub