PDA

View Full Version : [SOLVED:] Select sentence by searchgin for keyword and add comment



Frits
08-19-2013, 04:49 AM
Hi,

I'm trying the following. I want to search for a word, select the whole sentence and add a comment to the sentence.
My macro works, but only for the word I search for. How can I select the whole sentence?
Any help would be more than welcome.

Regards,
Frits


Sub Keywordselection()
'
' Select keywords and add automatic comment
'
Dim range As range
Set range = ActiveDocument.Content

Text = InputBox("Type a keyword you want to search for")

Do While range.Find.Execute(Text) = True
ActiveDocument.Comments.Add range, "This is the keyword incl sentence you have been searching for"
Loop
End Sub

Doug Robbins
08-19-2013, 06:19 PM
Use


Sub Keywordselection()
'
' Select keywords and add automatic comment
'
Dim range As range, rngfound As range
Set range = ActiveDocument.Content

Text = InputBox("Type a keyword you want to search for")

Do While range.Find.Execute(Text) = True
Set rngfound = range.Sentences(1)
ActiveDocument.Comments.Add rngfound, "This is the keyword incl sentence you have been searching for"
Loop
End Sub

Frits
08-20-2013, 12:16 AM
Great, thanks Doug!

It works good, however there is a small error when type certain key words. The error is:
"run-time error '4605': This method or property is not available because the object refers to the end of a table row."

How could I solve this?

Doug Robbins
08-20-2013, 01:12 AM
Try:


Sub Keywordselection()
'
' Select keywords and add automatic comment
'
Dim range As range, rngfound As range
Set range = ActiveDocument.Content

Text = InputBox("Type a keyword you want to search for")

Do While range.Find.Execute(Text) = True
Set rngfound = range.Sentences(1)
rngfound.End = rngfound.End - 2
ActiveDocument.Comments.Add rngfound, "This is the keyword incl sentence you have been searching for"
Loop
End Sub

Frits
08-21-2013, 02:36 AM
Great! It works. Thanks Doug.