Search code examples
vbams-wordword-2016polarion

MS Word 2016: Remove all protected comments using VBA


I would like to remove in an exported word document (.docx) all protected comments using VBA.

The comments are protected by some kind of fields they rely on (however I couldn't find a way to remove this protection with or without VBA). Those "fields" were generated when exporting from the application (Polarion ALM).

I was trying in the Word Macros the following to remove the protected comments:

Sub MakroRemoveComment()

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

ActiveDocument.DeleteAllComments

End Sub

but I ended up with following error message:

The method 'DeleteAllComments' for the object 'comment' failed.

I guess this is due to the protection of the comment fields.

Screenshot:

enter image description here


Solution

  • Just stumbled up on the question, for what it is worth the code to remove the protection of those elements ("Inhaltssteuerelemente" in german)

    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