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
vba ms-word word-vba ms-office automation
Christopher oezbek
source share