Excel 2019

Protected View Events

In this tutorial we’ll discuss the application events related to the Protected View. The Protected View is normally enabled for the files that you’ve downloaded from a website or from an email attachment. In Protected View, the most commands and editing functions are disabled to protect your computer from the viruses and malware. If you know the file is safe, you can exit Protected View and edit it as you would normally. To find out why Excel opened the file in Protected View, click File > Info:

Protected view information

  1. ProtectedViewWindowActivate
  2. ProtectedViewWindowBeforeClose
  3. ProtectedViewWindowBeforeEdit
  4. ProtectedViewWindowDeactivate
  5. ProtectedViewWindowOpen
  6. 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:

How to open a file in protected view

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)

Coding ProtectedViewWindowActivate

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)

Coding ProtectedViewWindowBeforeClose:
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:

  1. Pvw
    The ProtectedViewWindow object, represents the Protected View window.
  2. Reason
    The XlProtectedViewCloseReason 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.
  3. Cancel
    The Boolean data type, setting this variable to True 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)

Coding ProtectedViewWindowBeforeEdit

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:

  1. Pvw
    The ProtectedViewWindow object, represents the Protected View window.
  2. Cancel
    The Boolean data type, setting this variable to True 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)

Coding ProtectedViewWindowDeactivate

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)

Coding ProtectedViewWindowOpen

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)

Coding ProtectedViewWindowResize

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
Advertisement:
Advertisement: