Consulting

Results 1 to 12 of 12

Thread: How to highlight paragraphs and add comment

  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,316
    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 Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,743
    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
    Thanks Gmaxey. I can do it now.

  7. #7
    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.

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

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,316
    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

  10. #10
    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.

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,316
    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

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

Posting Permissions

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