I need help writing a macro that searches through a large document (100+ pages) to find certain keywords, and inserts a comment at each found instance. I would like to reference hundreds of keywords, but for simplicity, I've used three keywords for this example.
Found instances of Keyword X will have a specific comment.
Found instances of Keyword Y will have a specific comment.
Found instances of Keyword Z will have a specific comment... etc.
For example:
Keyword X appears in a Word document 19 times. To complicate matters, Keyword X can be variations of the same word (e.g., work, working, worked, works)
Keyword Y appears in a Word document 7 times.
Keyword Z appears in a Word document 54 times.
For each instance of keyword X, I would like to add a comment stating "Please replace [Keyword X] with vuvuzela." (where Keyword X is the actual value that's being passed through)
For each instance of keyword Y, I would like to add a comment stating "Please add a Copyright symbol after [Keyword Y]." (where Keyword Y is the actual value that's being passed through)
For each instance of keyword Z, I would like to add a comment stating "Please add a TM symbol after [Keyword Z]."(where Keyword Z is the actual value that's being passed through)
I found some helpful code here, which allows a user to "hard code" each keyword and display a comment.
However, it is cumbersome since I have to copy and paste the entire block for each keyword, and I have hundreds. Is there a way to have one block of code to loop through searching for keywords, and have a lengthy list of "canned" responses to add in the comment bubble?
Lastly, I cannot display the keyword in the comment without putting it the comment field.
Any help this community can provide is much appreciated. Thanks in advance!
Sub CommentBubble()
Application.ScreenUpdating = False
Dim i As Long, StrCmnt As String
With ActiveDocument.range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Keyword X"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = Yes
.Execute
End With
Do While .Find.Found
.Comments.Add range:=.Duplicate, Text:="Please replace [keyword X] with vuvuzela."
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
Application.ScreenUpdating = False
With ActiveDocument.range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Keyword Y"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
.Comments.Add range:=.Duplicate, Text:="Please add a Copyright symbol after [keyword Y]."
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
Application.ScreenUpdating = False
With ActiveDocument.range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Keyword Z"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
.Comments.Add range:=.Duplicate, Text:="Please add the TM symbol after [Keyword Z]."
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub