PDA

View Full Version : [SOLVED:] How to highlight paragraphs and add comment



KimLien
04-11-2023, 02:04 AM
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

gmayor
04-11-2023, 09:42 PM
After the line

oRng.Paragraphs(1).Range.HighlightColorIndex = wdYellow
add the line

oRng.Comments.Add oRng, "This is the text of your comment"

KimLien
04-12-2023, 05:32 AM
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

gmaxey
04-12-2023, 10:51 AM
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

Aussiebear
04-12-2023, 12:07 PM
@KimLien. Welcome to the VBAX forum. Are you wanting to add comments "on the fly" rather than a set text?

KimLien
04-14-2023, 05:38 AM
Thanks Gmaxey. I can do it now.

KimLien
04-14-2023, 05:50 AM
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.

KimLien
04-17-2023, 05:02 AM
Hi all,
I have one more question. How to change font style of comments.

gmaxey
04-17-2023, 06:32 AM
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

KimLien
04-17-2023, 02:46 PM
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.

gmaxey
04-17-2023, 04:37 PM
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.

KimLien
04-20-2023, 07:34 PM
Thank you so much for your detail explainations. Have a nice day
Kim Lien

tonyadams
11-02-2023, 02:29 AM
Hi Lien, crossover grid (https://wordleunlimited.online/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.

poultrydeman
01-08-2024, 09:05 PM
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 (https://snake-io.io/)
It's great to discuss diverse topics together. This provides a lot of valuable information.