Working with Document Protected View Events in Word 2010
Handy Programming Tips for Microsoft Word 2010: Learn how Protected View responds to various events in Microsoft Word 2010, and watch those responses as you interact with an open document.
Last modified: May 10, 2011
Applies to: Office 2010 | VBA | Word 2010
Published: May 2011
Provided by: Frank Rice, Microsoft Corporation
Protected View is one of the new security improvements that applications use in Microsoft Office 2010. It addresses the problem of how to identify documents that pose a risk to you and what to do with those documents. In this topic, you programmatically interact with a document that is opened in Protected View and observe the results of that interaction. To complete this task, you must do the following:
In this task, you add programming code that creates the Protected View events handlers. These routines are then used to write text to the document as each event occurs.
To add code to the Visual Basic Editor
On the Developer tab, click Visual Basic to open the Visual Basic Editor.
If you do not see the Developer tab in Word 2010, click the File tab, and then click Options. In the categories pane, click Customize Ribbon, select Developer, and then click OK.
In the Projects pane, click ThisDocument.
Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.
Private WithEvents app As Word.Application Private doc As Word.Document Private Sub AddText(text As String) doc.Content.InsertParagraphAfter doc.Content.InsertAfter (text) End Sub Private Sub app_ProtectedViewWindowActivate(ByVal PvWindow As ProtectedViewWindow) AddText "ProtectedViewWindowActivate: " & PvWindow.Caption End Sub Private Sub app_ProtectedViewWindowBeforeClose(ByVal PvWindow As ProtectedViewWindow, ByVal CloseReason As Long, Cancel As Boolean) ' You can cancel the ProtectedViewWindowBeforeClose event. Dim reason As String reason = "None" Select Case CloseReason Case wdProtectedViewCloseEdit reason = "Edit" Case wdProtectedViewCloseForced reason = "Forced" Case wdProtectedViewCloseNormal reason = "Normal" End Select Dim text As String text = "ProtectedViewWindowBeforeClose: " & _ PvWindow.Caption & "(" & reason & ")" AddText text Cancel = (MsgBox(text, vbOKCancel, "Event") = vbCancel) End Sub Private Sub app_ProtectedViewWindowBeforeEdit(ByVal PvWindow As ProtectedViewWindow, Cancel As Boolean) ' You can cancel the ProtectedViewWindowBeforeEdit event. Dim text As String text = "ProtectedViewWindowBeforeEdit: " & PvWindow.Caption AddText text Cancel = (MsgBox(text, vbOKCancel, "Event") = vbCancel) End Sub Private Sub app_ProtectedViewWindowDeactivate(ByVal PvWindow As ProtectedViewWindow) AddText "ProtectedViewWindowDeactivate: " & PvWindow.Caption End Sub Private Sub app_ProtectedViewWindowOpen(ByVal PvWindow As ProtectedViewWindow) AddText "ProtectedViewWindowOpen: " & PvWindow.Caption End Sub Private Sub app_ProtectedViewWindowSize(ByVal PvWindow As ProtectedViewWindow) AddText "ProtectedViewWindowSize: " & PvWindow.Caption End Sub Private Sub Document_Open() Set doc = Word.ActiveDocument Set app = Word.Application End Sub
Close the Visual Basic Editor.
Close and save the document as a macro-enabled file. Reopen the document to run the code in the Document_Open event and initialize the other event routines.
In this task, you open an existing document in Protected View and interact with the document. Your interaction triggers various events and writes text to the document so that you can see which effect raises which event.
To run the code
From the open document, open another document in Protected View by opening it from the Internet or by using the following steps.
Click File and then click Open.
In the Open dialog box, locate an existing document, but do not open it.
Click the arrow next to the Open button, and then select Open in Protected View from the menu to open the document in Protected View.
Interact with the document that is open in Protected View; resize it, switch the focus away and then back to the document, close it, and so on. Watch the text that appears in the current document in response to your actions.