VBA文档保护未设置wdProtectionType(Office 2007)

8
我希望能够使用Office 2007 VBA的Document.Protect自动化保护Word文档,仅供评论使用。如果文档以前从未进行过保护,则此方法运行良好,但是一旦已设置保护,它将失败。
以下是一个最小化的工作示例,展示了我所面临的问题(见下文)。我已在另一台运行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
谢谢你的帮助!它对你有用吗?在哪个Office版本中? - Christopher Oezbek
1
跟进SP 3。如果用户在手动设置其他保护类型之前没有问题,代码似乎是有效的。虽然可以取消保护,但新的保护类型不会应用。 - Christopher Oezbek
我只能使用AHK从VBA命令中调用。你能发一个例子吗? - Christopher Oezbek
VBA有点繁琐。这个链接应该会有所帮助:https://msdn.microsoft.com/zh-cn/library/dd819387(v=office.12).aspx - DanL
显示剩余12条评论
1个回答

2

在进行了一些在线研究并且代码多次失败后,我找到了一篇解决我的问题的帖子。这个问题是由于Word在阅读视图中打开受保护的文档引起的。

这是原始帖子的链接链接

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

我希望这可以帮到您:)

我能理解那可能是个问题,但我已经禁用了“阅读版式”打开选项并改进了示例代码。然而,在我的Office 2007中仍然存在这个问题。if (oDoc.ActiveWindow and oDoc.ActiveWindow.View.ReadingLayout) oDoc.ActiveWindow.View.ReadingLayout = False - Christopher Oezbek
很遗憾,这个答案在Office 2007下并没有帮助。 - Christopher Oezbek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接