MS Word 2016:使用VBA删除所有受保护的注释

时间:2018-10-29 16:16:05

标签: vba ms-word word-2016 polarion

我想使用VBA在导出的Word文档(.docx)中删除所有受受保护的注释。

注释受它们所依赖的某种字段的保护(但是无论有没有VBA,我都找不到消除这种保护的方法)。这些“字段”是从应用程序(Polarion ALM)导出时生成的。

我正在Word宏中尝试以下操作以删除受保护的注释:

Sub MakroRemoveComment()

If ActiveDocument.ProtectionType <> wdNoProtection Then
 ActiveDocument.Unprotect Password:=strPassword
End If

ActiveDocument.DeleteAllComments

End Sub

但是我最终收到以下错误消息:

  

对象'comment'的方法'DeleteAllComments'失败。

我想这是由于对评论字段的保护。

屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:1)

只是偶然发现了一个问题,即删除这些元素的保护的代码值得做什么(德语中的“ Inhaltssteuerelemente”)

Private Sub UnprotectDocument()
    Set doc = ActiveDocument
' Unprotect the document
    If doc.ProtectionType <> wdNoProtection Then
        doc.Unprotect
        doc.Protect Type:=wdNoProtection
    End If

' Remove lock from Inhaltssteuerelementen --> Wiki Content
' Iterate through all the content controls in the document
' and remove locks
    Dim cc As ContentControl
    If doc.ContentControls.Count <> 0 Then
    For Each cc In doc.ContentControls
        cc.LockContentControl = True
        cc.LockContents = False
    Next
    End If
End Sub