- ProtectedViewWindowActivate
- ProtectedViewWindowBeforeClose
- ProtectedViewWindowBeforeEdit
- ProtectedViewWindowDeactivate
- ProtectedViewWindowOpen
- ProtectedViewWindowResize
If you suspect a file that you have to open may contains malware, open it in Protected View. To open a file in Protected View, Click File > Open > Browse
and choose Open in Protected View
from the Open
drop-down menu:
The Protected View events are Applications-level events. To program these events you need to create an object variable that represents the Excel’s Application object and propagate its events. Let’s open a workbook and press Alt+F11
to open the VBE. Declares the App
variable in the ThisWorkbook
code module and use the Workbook_Open()
event procedure to set the App
object variable to reference the current Excel window. Next, close the workbook (save changes) and then re-open it. For more information see How to Listen Application Events:
Option Explicit Dim WithEvents App As Application Private Sub Workbook_Open() Set App = Application End Sub
ProtectedViewWindowActivate
Syntax: App_ProtectedViewWindowActivate(Pvw)
The ProtectedViewWindowActivate
event occurs when a Protected View window is activated. This event has one argument Pvw
(the ProtectedViewWindow
object) which represents the window being activated.
App_ProtectedViewWindowActivate
Example:
The ProtectedViewWindowActivate
event occurs when you open any Protected View workbook or when you activate any Protected View workbook by switching it from another workbook, try following example:
Option Explicit Dim WithEvents App As Application Private Sub App_ProtectedViewWindowActivate(ByVal Pvw As ProtectedViewWindow) MsgBox Pvw.SourceName End Sub Private Sub Workbook_Open() Set App = Application End Sub
ProtectedViewWindowBeforeClose
Syntax: App_ProtectedViewWindowBeforeClose(Pvw, Reason, Cancel)
:
You must close and reopen the workbook or the event will not be captured.
The ProtectedViewWindowBeforeClose
event occurs immediately before a Protected View window is closed. This event has three arguments:
Pvw
TheProtectedViewWindow
object, represents the Protected View window.Reason
TheXlProtectedViewCloseReason
Enum constant, specifies how the Protected View window was closed, this variable has following possible values:xlProtectedViewCloseEdit
The window was closed when the user clicked the Enable Editing button.xlProtectedViewCloseForced
The window was closed because the application shut it down forcefully or stopped responding.xlProtectedViewCloseNormal
The window was closed normally.
Cancel
The Boolean data type, setting this variable toTrue
prevents the windows / workbook from closing.
App_ProtectedViewWindowBeforeClose
Example:
Option Explicit Dim WithEvents App As Application Private Sub App_ProtectedViewWindowBeforeClose(ByVal Pvw As ProtectedViewWindow, ByVal Reason As XlProtectedViewCloseReason, Cancel As Boolean) If Reason = xlProtectedViewCloseEdit Then MsgBox "Enabled Editing" ElseIf Reason = xlProtectedViewCloseForced Then MsgBox "Application Closed " Else MsgBox "Protected windows closed normally" End If End Sub Private Sub Workbook_Open() Set App = Application End Sub
ProtectedViewWindowBeforeEdit
Syntax: App_ProtectedViewWindowBeforeEdit(Pvw, Cancel)
The ProtectedViewWindowBeforeEdit
event occurs immediately before editing is enabled (when the user clicks the Enable Editing button) on the workbook in the specified Protected View window. This event has two arguments:
Pvw
TheProtectedViewWindow
object, represents the Protected View window.Cancel
The Boolean data type, setting this variable toTrue
prevents the workbook from editing.
App_ProtectedViewWindowBeforeEdit
Example:
Following code refused the editing of Protected View’s workbook:
Option Explicit Dim WithEvents App As Application Private Sub App_ProtectedViewWindowBeforeEdit(ByVal Pvw As ProtectedViewWindow, Cancel As Boolean) Cancel = True MsgBox "Enable Editing is disabled " End Sub Private Sub Workbook_Open() Set App = Application End Sub
ProtectedViewWindowDeactivate
Syntax: App_ProtectedViewWindowDeactivate(Pvw)
The ProtectedViewWindowDeactivate
events occurs when the user deactivates a workbook in Protected View mode. This procedure has one argument Pvw
(the ProtectedViewWindow
object) which represent the workbook being deactivated.
App_ProtectedViewWindowDeactivate
Example:
Following code executes when the Protected View workbook deactivates, for example when you change focus to another workbook or close the workbook:
Option Explicit Dim WithEvents App As Application Private Sub App_ProtectedViewWindowDeactivate(ByVal Pvw As ProtectedViewWindow) MsgBox Pvw.SourceName & " deactivated" End Sub Private Sub Workbook_Open() Set App = Application End Sub
ProtectedViewWindowOpen
Syntax: App_ProtectedViewWindowOpen(Pvw)
The ProtectedViewWindowOpen
event occurs when a workbook is opened in a Protected View. This procedure has one argument Pvw
(the ProtectedViewWindow
object) which is the workbook being opened.
App_ProtectedViewWindowOpen
Example:
Option Explicit Dim WithEvents App As Application Private Sub App_ProtectedViewWindowOpen(ByVal Pvw As ProtectedViewWindow) MsgBox Pvw.SourceName & " opened" End Sub Private Sub Workbook_Open() Set App = Application End Sub
ProtectedViewWindowResize
Syntax: App_ProtectedViewWindowResize(Pvw)
The ProtectedViewWindowResize
event occurs when when any Protected View window is resized. This procedure has one argument Pvw
(the ProtectedViewWindow
object) which is the workbook being opened.
App_ProtectedViewWindowResize
Example:
The code executes when you resize, minimize or maximize the Protected View window:
Option Explicit Dim WithEvents App As Application Private Sub App_ProtectedViewWindowResize(ByVal Pvw As ProtectedViewWindow) MsgBox Pvw.SourceName & " resized" End Sub Private Sub Workbook_Open() Set App = Application End Sub