none
Delete Duplicate word Comments RRS feed

  • Question

  • Hi all,

    I would like to know is there any way to compare and remove duplicate comments leaving only the first one.

    For example 

    ============================================

    Paragraph 1 comment is Para

    Paragraph 2 comment is Para

    Paragraph 3 comment is Para

    Paragraph 4 comment is NewPara

    Paragraph 5 comment is Para

    Paragraph 6 comment is Para

    Paragraph 7 comment is Para

    ============================================

    Now what i expect is to remove every duplicate comment except the comment in paragraph 1 and paragraph 5. Right now I couldn't find anything to achieve this. Because working with comments in VBA is bit different I think.  

    Any help would be much appreciated.

    Regards,

    __________________________________

    Supun Samarakoon


    • Edited by Supunsam Wednesday, September 7, 2016 4:04 PM
    Tuesday, September 6, 2016 5:50 PM

Answers

  • Hi Supunsam,

    You can use the code mentioned below.

    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
    

    you can see in the picture below that i have several paragraphs in Document and i added a duplicate comments there.

    after running the code i get output like below. you can see that duplicate comments are deleted except the original comment.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 7, 2016 2:08 AM
    Moderator

All replies

  • Hi Supunsam,

    You can use the code mentioned below.

    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
    

    you can see in the picture below that i have several paragraphs in Document and i added a duplicate comments there.

    after running the code i get output like below. you can see that duplicate comments are deleted except the original comment.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 7, 2016 2:08 AM
    Moderator
  • Hi Deepak,

    Thank you very much for your contribution. I work just fine with the Simple scenario which I have shown here. But in my document this is not working exactly to the expectation. I've found similar macro in: http://www.thespreadsheetguru.com/the-code-vault/remove-duplicate-paragraphs-from-entire-word-document

    But its removing some necessary comments too. I really appreciate your help.

    Is there another approach to check delete duplicate comments?

    Regards,

    _____________________________
    Supun Samarakoon


    Regards, __________________________________ Supun Samarakoon

    Wednesday, September 7, 2016 4:18 PM
  • Hi Spunsam,

    you had mentioned that, "But its removing some necessary comments too."

    did you mean you want to keep some of the duplicate comments?

    if so then you need to store the paragraph number.

    and check in the loop when it find that paragraph at the time of execution. it will skip to next paragraph.

    thus you can keep your duplicate comments.

    if there are many duplicate comments then you can store the paragraph numbers in an array.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 8, 2016 8:56 AM
    Moderator