我希望能够使用Office 2007 VBA的Document.Protect自动化保护Word文档,仅供评论使用。如果文档以前从未进行过保护,则此方法运行良好,但是一旦已设置保护,它将失败。
以下是一个最小化的工作示例,展示了我所面临的问题(见下文)。我已在另一台运行Office 2007 SP3的PC上复制了此问题。即使使用空白文档,该问题仍会发生。
要重现,请在保存新的空白文档后使用以下宏:
以下是一个最小化的工作示例,展示了我所面临的问题(见下文)。我已在另一台运行Office 2007 SP3的PC上复制了此问题。即使使用空白文档,该问题仍会发生。
要重现,请在保存新的空白文档后使用以下宏:
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
调查的事项:
- Office 2007已更新至SP3和最新的Windows更新
- 如果手动执行保护类型可以正确更改,但记录为宏失败。
- 其他类型的文档保存(Document.Save或Document.SaveAs(2))
- 禁用ReadingLayout
ActiveWindow.View.ReadingLayout = False
(参见Alredo的答案):在Office 2007中没有变化
编辑:
- 2015-10-23:初始问题
- 2015-10-25:添加了最小工作示例。
- 2015-10-25:发现只有在手动或编程设置保护类型后才无法再更改。
- 2015-10-26:提供赏金
ActiveDocument
在运行时很容易改变。 - R3uK