Option Explicit
Public WithEvents oApp As Excel.Application
Private bDeferredOpen As Boolean
Private Sub oApp_WorkbookActivate(ByVal Wb As Workbook)
If bDeferredOpen
Then bDeferredOpen = False Call WorkbookOpenHandler(Wb)
End If End Sub
Private Sub oApp_WorkbookOpen(ByVal Wb As Workbook)
Dim oProtectedViewWindow As ProtectedViewWindow On Error Resume Next 'The below line will throw error (Subscript out of range)
if the workbook is not opened in protected view.
Set oProtectedViewWindow = oApp.ProtectedViewWindows.Item(Wb.Name) On Error GoTo 0 'Reset error handling
If oProtectedViewWindow Is Nothing
Then bDeferredOpen = False Call WorkbookOpenHandler(Wb)
Else 'Delay open actions till the workbook gets activated. bDeferredOpen = True
End If End Sub
Private Sub WorkbookOpenHandler(ByVal Wb As Workbook)'The actual workbook open event handler code goes here...
End Sub