Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  2. #2
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Wellington, New Zealand
    Posts
    143
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by amakeler View Post
    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.

    Quote Originally Posted by amakeler View Post
    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

  4. #4
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Great! Yes, that works!

    And the 'backwards' rule is an important general rule to learn.

    Thanks!

    avi



    Quote Originally Posted by William View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •