Search code examples
excelvbams-wordcomments

Delete all comments of Wordfile


so I'm trying to delete all comments in a Wordfile per VBA-Code of an Excelfile.

I've tried using

'Dim ObjWord as Word.Application
ObjWord.ActiveDocument.DeleteAllComments

and calling

Sub RemoveAllComments(Doc As Document)
    Dim n As Long
    Dim oComments As Comments
    Set oComments = Doc.Comments
    For n = oComments.Count To 1 Step -1
    oComments(n).Delete
    Next
    Set oComments = Nothing
End Sub

but the first gave me the run-time 'error 4605 command not available' and the second code segment throws an 'error 438 object doesn't support this property or method'.

Is there any other way I could do it?

Edit 1:

Dim ObjWord As Word.Application
Set ObjWord = LoadWord()
ObjWord.Visible = True

is in the Function and it calls LoadWord():

Function LoadWord() As Word.Application 
    Set LoadWord = GetObject(, "Word.Application")
    If MsgBox("Word's already in use klick ok to dismiss all changes", vbOKCancel) = vbCancel Then
        Set LoadWord = Nothing
    End If
    Exit Function
End Function

Solution

  • Error 4605 with:

    ObjWord.ActiveDocument.DeleteAllComments
    

    indicates the ActiveDocument (if there is one) contains no comments. Since you haven't posted any code relating to how the ActiveDocument (or Doc, in your second sub) is instantiated, or whether you've established that the document concerned even contains any comments, it is impossible to give further advice in that regard.

    Furthermore, this:

    Sub RemoveAllComments(Doc As Document)
    

    would be invalid. At the very least it would need to be:

    Sub RemoveAllComments(Doc As ObjWord.Document)
    

    For the first example, the situation can be remedied with nothing more complicated than:

    On Error Resume Next
    ObjWord.ActiveDocument.DeleteAllComments
    On Error Goto 0