VBA Document.Protect does not install wdProtectionType (Office 2007)

I would like to automate the process of protecting a text document for comments only using Office 2007 VBA Document.Protect. This works great if the document is not already protected, but does not work as soon as protection has been previously set.

Below is a minimal working example showing the error I am encountering (see below). I reproduced on another PC with Office 2007 Service Pack 3 (SP3). The problem occurs even when using a blank document.

To reproduce, use the following macro after saving a new blank document:

Sub ProtectionBugOffice2007() ' Apply a first type of locking to simulate an existing lock RecentFiles(1).Open If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect ActiveDocument.Protect wdAllowOnlyFormFields ActiveDocument.Close (wdSaveChanges) ' Now do the real test: Lock with our intended protection type RecentFiles(1).Open ActiveDocument.Unprotect ActiveDocument.Protect wdAllowOnlyComments ActiveDocument.Close (wdSaveChanges) ' Validate! RecentFiles(1).Open If ActiveDocument.ProtectionType = wdAllowOnlyComments Then MsgBox "Success!" Else MsgBox "Failure! Should be " & wdAllowOnlyComments & " but is " & ActiveDocument.ProtectionType End If ActiveDocument.Close End Sub 

All that has been investigated before:

  • Office 2007 Updated with SP 3 Update and Latest Version Upgrade
  • If the manually executed type of protection can be changed correctly, but recorded as a macro, it fails.
  • Other types of document saving (Document.Save or Document.SaveAs (2))
  • Disabling ReadLayout ActiveWindow.View.ReadingLayout = False (see Alredo's answer): no change in Office 2007

edits:

  • 2015-10-23: The original problem
  • 2015-10-25: Added a minimal working example.
  • 2015-10-25: It was found that only after manual or software setting of the type of protection it can no longer be changed.
  • 2015-10-26: Suggested Award
+7
vba ms-word word-vba ms-office automation
source share
1 answer

After doing some research online and the code doesn’t work for me several times. I found a message that solved my problem, which was caused by the word opening protected documents while reading.

This is the link to the original message. Link to the message

 Sub ProtectionBugOffice2007() Dim oDoc As Document ' Apply a first type of locking to simulate an existing lock Set oDoc = OpenRecentNotReadOnly If oDoc.ProtectionType <> wdNoProtection Then oDoc.Unprotect oDoc.Protect wdAllowOnlyFormFields oDoc.Close (wdSaveChanges) ' Now do the real test: Lock with our intended protection type Set oDoc = OpenRecentNotReadOnly oDoc.Unprotect oDoc.Protect wdAllowOnlyComments oDoc.Close (wdSaveChanges) ' Validate! Set oDoc = OpenRecentNotReadOnly If oDoc.ProtectionType = wdAllowOnlyComments Then MsgBox "Success!" Else MsgBox "Failure! Should be " & wdAllowOnlyComments & " but is " & oDoc.ProtectionType End If oDoc.Close End Sub ' Function to open the document not in read only. Function OpenRecentNotReadOnly() As Document Dim ret As Document Set ret = RecentFiles(1).Open ActiveWindow.View.ReadingLayout = False 'Return the value Set OpenRecentNotReadOnly = ret End Function 

Hope this helps :)

+2
source share

All Articles