How do I remove all comments from the active writer document

1k views Asked by At

I am trying to write a macro on my OpenOffice Writer to remove all comments from the active word document, so far the code I have actually researched and cobbled together can be found below.

Sub RemoveAllComments()

    Dim i as Integer

    For i = doc.Comments.Count To 1 Step -1
        doc.Comments(i).Delete
    Next i

End sub

My compiler is giving me an "Object variable not set" error and I am not sure how that applies to the code I have cobbled together. I am really new to this and I don't even know what the To 1 Step -1 even means!

I would really appreciate your help!

1

There are 1 answers

1
jmstoker On

For OpenOffice Writer 4.0.0 the following code is confirmed to delete all comments in the document.

sub RemoveAllComments2
    rem ----------------------------------------------------------------------
    rem Define variables
    dim document   as object
    dim dispatcher as object
    rem ----------------------------------------------------------------------
    rem Get access to the document
    document   = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

    rem ----------------------------------------------------------------------
    dispatcher.executeDispatch(document, ".uno:DeleteAllNotes", "", 0, Array())
end sub

You're original code is for Microsoft Word and I have provided a solution in that case as well. For Word 2007, 2010, and 2013 the following code will remove all comments in the active document.

Sub RemoveAllComments()
    Dim n As Long
    For n = ActiveDocument.Comments.Count To 1 Step -1
    ActiveDocument.Comments(n).Delete
    Next 'n
End Sub

The Count To 1 Step -1 Is saying to start at Count and count backwards to 1 with increments of 1.