Consulting

Results 1 to 14 of 14

Thread: How to highlight paragraphs and add comment

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    How to highlight paragraphs and add comment

    I have macro to find text then highlight paragraphs having it.
    Now i want to add comment to that paragraphs. But dont know how.
    Can anyone suggest.

    Here is code for Highlight

    Sub Highlight_WordN()
    Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
       Do While .Execute(FindText:="HAPPY")
       oRng.Paragraphs(1).Range.HighlightColorIndex = wdYellow
       oRng.Collapse 0
       Loop
    End With
    lbl_Exit:
    Set oRng = Nothing
    Exit Sub
    End Sub
    Thank you.
    Lien
    Last edited by Aussiebear; 04-11-2023 at 04:58 AM. Reason: Added code tags to supplied code

  2. #2
    After the line
    oRng.Paragraphs(1).Range.HighlightColorIndex = wdYellow
    add the line
    oRng.Comments.Add oRng, "This is the text of your comment"
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thank you Gmayor for your reply.

    But i mean, i want to add comment to the paragraph, not only the text.
    Can you please spend sometime to help.

    Thank you.
    Kim Lien

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,382
    Location
    Sub Highlight_WordN()
    Dim oRng As Range
    Dim oComment As Comment
      Set oRng = ActiveDocument.Range
      With oRng.Find
        Do While .Execute(FindText:="HAPPY")
          oRng.Paragraphs(1).Range.HighlightColorIndex = wdYellow
          Set oComment = ActiveDocument.Comments.Add(oRng.Paragraphs(1).Range, "Whatever text you want as the comment")
          oRng.Collapse 0
        Loop
      End With
    lbl_Exit:
      Set oRng = Nothing
      Set oComment = Nothing
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,180
    Location
    @KimLien. Welcome to the VBAX forum. Are you wanting to add comments "on the fly" rather than a set text?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Hi Ausiebear,
    I'm glad to be here.
    Yes, there is one thing that I need to add comment to paragraph as fast as possible.

  7. #7
    Quote Originally Posted by KimLien View Post
    Hi Ausiebear,
    I'm glad to be here.
    Yes, there is one thing that I need to add comment to paragraph as fast as possible.
    snake io
    It's great to discuss diverse topics together. This provides a lot of valuable information.

  8. #8
    Thanks Gmaxey. I can do it now.

  9. #9
    Hi all,
    I have one more question. How to change font style of comments.

  10. #10
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,382
    Location
    Add a line of code:
    Sub Highlight_WordN()
    Dim oRng As Range
    Dim oComment As Comment
      Set oRng = ActiveDocument.Range
      With oRng.Find
        Do While .Execute(FindText:="HAPPY")
          oRng.Paragraphs(1).Range.HighlightColorIndex = wdYellow
          Set oComment = ActiveDocument.Comments.Add(oRng.Paragraphs(1).Range, "Whatever text you want as the comment")
          oComment.Range.Style = "Body Text" 'Or whatever built-in or custom named style you want to apply.
          oRng.Collapse 0
        Loop
      End With
    lbl_Exit:
      Set oRng = Nothing
      Set oComment = Nothing
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  11. #11
    Hi Gmaxey
    I got Message: 'Item with specified name not exist' when run the code

    oComment.Range.Style= "Times New Roman"
    then i try this and it works.

    oComment.Range.Style.Font.Name = "Times New Roman"
    Thank you very much.

  12. #12
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,382
    Location
    You get the error because there is no builtin or custom style named "Times New Roman" on your PC. As you have seen, ".Font" is a property of the style object. "Times New Roman" is the name for one of fonts installed on your machine. You can create a style e.g., "MyComments" at set all of the properties as you wish then apply that style using the line of code that I provided.
    Greg

    Visit my website: http://gregmaxey.com

  13. #13
    Thank you so much for your detail explainations. Have a nice day
    Kim Lien

  14. #14
    Hi Lien, crossover grid
    You can add comment to that paragraphs following this code:
    Sub Highlight_And_AddComment()    
    Dim oRng As Range
    Dim oDoc As Document
    Dim oPara As Paragraph
    Dim oComment As Comment
    Set oDoc = ActiveDocument
    ' Initialize the range to the start of the document
    Set oRng = oDoc.Range
    oRng.Collapse wdCollapseStart
    ' Loop through the document to find and highlight the text
    Do While oRng.Find.Execute(FindText:="HAPPY")
         ' Highlight the entire paragraph in yellow
        oRng.Paragraphs(1).Range.HighlightColorIndex = wdYellow
        ' Add a comment to the paragraph
        Set oPara = oRng.Paragraphs(1)
        Set oComment = oPara.Range.Comments.Add(Range:=oPara.Range, Text:="This paragraph contains the word 'HAPPY'")
        ' Move to the end of the document to continue searching
        oRng.Collapse wdCollapseEnd
    Loop
    End Sub


    Remember to replace "This paragraph contains the word 'HAPPY'" in the `Text` property of the `Comments.Add` method with your desired comment text.

Posting Permissions

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