none
Removing duplicate comments in Word VBA RRS feed

  • Question

  • Hi, 

    I use a macro that searches and comments based on criterias with using Wildcards. For som reason, which I can't figure out, it duplicates some of the comments. Is there a way to search for words that have duplicated comments, and remove the duplicates? 

    In example: 

    the macro comments (Don't use "blarp") on every usage of the word "blarp" in a document. For some reason the comments are added twice. If I use the macro posted beneath, it will delete all comments, leaving just one. I want it to only delete the duplicate comment for each occurance. Is this possible? 


    Sub DemoDeleteComment()

    Dim doc As Document
    Dim c1, c2 As Comment
    Dim DupCount As Long

    DupCount = 0

    For Each c1 In ActiveDocument.Comments
      If c1.Range.Text <> vbCr Then
        
        For Each c2 In ActiveDocument.Comments
          If c1.Range.Text = c2.Range.Text Then
            DupCount = DupCount + 1
            If c1.Range.Text = c2.Range.Text And DupCount > 1 Then c2.Delete
          End If
        Next c2
        
      End If
      
      
        DupCount = 0

    Next c1
    MsgBox "All Duplicate comments removed"
    End Sub

    Sunday, June 3, 2018 8:33 AM

Answers

  • Try this version:

    Sub DemoDeleteComment()
        Dim i As Long
        Dim j As Long
        Dim n As Long
        Dim s As String
        Application.ScreenUpdating = False
        n = ActiveDocument.Comments.Count
        For i = n To 2 Step -1
            s = ActiveDocument.Comments(i).Range.Text
            For j = 1 To i - 1
                If ActiveDocument.Comments(j).Range.Text = s Then
                    ActiveDocument.Comments(i).DeleteRecursively
                    Exit For
                End If
            Next j
        Next i
        Application.ScreenUpdating = True
        MsgBox "All duplicate comments removed", vbInformation
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, June 3, 2018 9:21 AM

All replies

  • Try this version:

    Sub DemoDeleteComment()
        Dim i As Long
        Dim j As Long
        Dim n As Long
        Dim s As String
        Application.ScreenUpdating = False
        n = ActiveDocument.Comments.Count
        For i = n To 2 Step -1
            s = ActiveDocument.Comments(i).Range.Text
            For j = 1 To i - 1
                If ActiveDocument.Comments(j).Range.Text = s Then
                    ActiveDocument.Comments(i).DeleteRecursively
                    Exit For
                End If
            Next j
        Next i
        Application.ScreenUpdating = True
        MsgBox "All duplicate comments removed", vbInformation
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, June 3, 2018 9:21 AM
  • Hi Morten,

    Do you have any issue with this thread?

    If not, I would suggest you mark the helpful reply as answer which is the way to close a thread here.

    If you do, please feel free to let us know.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 7, 2018 7:22 AM