• Word 2003 – VBA – to delete selected comments

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Word 2003 – VBA – to delete selected comments

    Author
    Topic
    #471049

    Hi all,

    RE: Word 2003 – VBA – To delete all comments within an area of text

    1. Is there a user operation available to delete all comments within just an area of text I select of a document?

    2. Well I couldn’t find a user operation that does the above, so I tried to write a VBA routine that deletes all comments that I select – see below. However it insists on deleting all the comments in the document up to and including all the comments within my selection. Can anybody make that work?

    Code:
    Sub Comments_DeleteAllSelected()
        Dim Cmnt As Comment   
        For Each Cmnt In Selection.Comments
            Cmnt.Delete
        Next Cmnt
    End Sub

    3. I macro-recorded myself deleting a single comment that I selected (i.e., I selected the comment itself, not an area of text in Word), and it recorded the statement, WordBasic.RejectChangesSelected, but when I tried to run that in my own code, I got the error message: “this command is not available”.

    TIA

    avi

    Viewing 1 reply thread
    Author
    Replies
    • #1239417

      The following code works for me, using Word 2003.

      Code:
        Dim i As Integer
      
        With Selection.Comments
          For i = .Count To 1 Step -1
            .Item(i).Delete
          Next i
        End With
      • #1239490

        Great! Yes, that works!

        And the ‘backwards’ rule is an important general rule to learn.

        Thanks!

        avi

        The following code works for me, using Word 2003.

        Code:
          Dim i As Integer
        
          With Selection.Comments
            For i = .Count To 1 Step -1
              .Item(i).Delete
            Next i
          End With
    • #1239426

      Is there a user operation available to delete all comments within just an area of text I select of a document?

      I don’t think so. Unlike some other items that appear on the Special pop-up in the Replace dialog, I don’t think you can quickly zap out comments.

      However it insists on deleting all the comments in the document up to and including all the comments within my selection.

      As William’s code excerpt illustrates, it is safest to remove items from the end of a collection rather than the beginning. Otherwise, Word may skip over alternate items (or maybe reach beyond the selection??). Also, because the selection could change during the operation, you might want to create a temporary range and operate on that. For example:

      Code:
      Sub RemoveCommentsFromSelection()
      Dim rngTemp As Word.Range
      ' Create range object from the Selection
      Set rngTemp = Selection.Range
      With rngTemp
          ' Delete all comments in the range,
          '  from the end to the beginning
          While .Comments.Count > 0
              .Comments(.Comments.Count).Delete
          Wend
      End With
      ' Restore selection just in case
      rngTemp.Select
      ' Clean up object reference
      Set rngTemp = Nothing
      End Sub
    Viewing 1 reply thread
    Reply To: Word 2003 – VBA – to delete selected comments

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: