PDA

View Full Version : Find Word then Highlight Entire Paragraph and or Sentence Around That Word



ChrisOK
09-07-2016, 10:48 AM
Need help improving this code to find a word/term, then highlight (yellow) the whole sentence containing that word or if it is easier to accomplish: highlighting the whole paragraph where that word exists would also be awesome!

Can be 2 separate chunks of code or maybe it's just a matter of changing a couple of things to make it go from only doing a SENTENCE vs. doing a PARAGRAPH.

I found refs of maybe using a "paragraph.range.select" and also found a note that advised these "start" and "end" terms to select a paragraph.. but not sure how to achieve this? Somewhat of a novice.
Selection.StartOf Unit:=wdParagraphm
Selection.MoveEnd Unit:=wdParagraph

Here's what I'm using now to FIND a word/term and do some highlighting but it is not highlighting the whole sentence (nor) whole paragraph.. perhaps it can be improved upon to work as desired or if not - I'm happy to trash it and use something better!


Sub Highlight_WordN()
'THIS FINDS A SPECIFIED TERM AS NOTED IN QUOTES BELOW AND HIGHLIGHTS IT IN YELLOW
'IT HIGHLIGHTS SEVERAL OF THE WORDS AROUND IT BUT DOES NOT QUITE PICK UP THE FULL PARAGRAPH I'M LOOKING TO HIGHLIGHT
'NEED TO EDIT IT A BIT TO BE MORE INCLUSIVE

Dim sFindText As String
'Start from the top of the document
Selection.HomeKey wdStory

sFindText = "Contractor Shall"
Selection.Find.Execute sFindText
Do Until Selection.Find.Found = False
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.range.HighlightColorIndex = wdYellow
Selection.MoveRight
Selection.Find.Execute
Loop
End Sub

gmayor
09-07-2016, 10:51 PM
The following should work

Sub Highlight_WordN()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="Contractor Shall")
oRng.Sentences(1).HighlightColorIndex = wdYellow
oRng.Collapse 0
Loop
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

ChrisOK
09-08-2016, 10:15 AM
Thanks Graham! This is pretty awesome! It is indeed highlighting MORE than it was with the old code so I'll definitely start using this updated code and trash the other .. but it's still not highlighting to end of paragraph in every case.. Not sure why? (see attached image)

Perhaps someone can expand it further to include through to end where a paragraph mark exists??
Here's a pic of what the better code you provided is doing (which again is awesome but check out where the Paragraph marks exist - in most cases it worked but appears there are 2 cases where it stopped at end of sentence and did not continue to highlight through to the end of the paragraph)
17051

gmaxey
09-08-2016, 12:11 PM
Sub Highlight_WordN()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="Contractor Shall")
oRng.Paragraphs(1).Range.HighlightColorIndex = wdYellow
oRng.Collapse 0
Loop
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

ChrisOK
09-08-2016, 12:44 PM
FABULOUS Greg!
It works - highlights to the end of the paragraph every time! So excited!
But not seeing how it differs from the other one above --- although this one worked fully and the other did not..?

I saw refs about defining a range w/ what you used: Rng.Paragraphs(1).Range but had no clue how to pull it together..
The (1) means to just do 1 paragraph right? (starting with the target word - go to the end of 1 paragraph)
Also, was not sure how the "collapse" command works.. Is this essentially saying: STOP/END but what's the "0" at the end of that signify?
and finally, the "lbl_Exit" can you expand/comment out what this is all doing so I can hopefully learn from it!

Thanks greatly -- SO - SO - SO appreciate your help and will cross-post it (after your response) to ensure other newbies like myself are helped by your solution & comments.
Chris

Attached is the result of what this new code does:
17052

gmaxey
09-08-2016, 01:34 PM
Actually in doesn't do exactly as you asked because for some reason the code I posted wasn't the code I wrote. I believe it if Contractor shall is found in the second
or subsequent sentence of a paragraph then you only want from that sentence on to the end of the paragraph highlighted.

It differed where Graham highlighted the sentence containing the found text where I highlighted the paragraph of the found text.


Sub Highlight_WordN()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="Contractor Shall")
oRng.Start = oRng.Sentences(1).Start
oRng.End = oRng.Paragraphs(1).Range.End
oRng.HighlightColorIndex = wdYellow
oRng.Collapse 0
Loop
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

Note: Word doesn't have a clear idea what a sentence is all about. Where the code above would highlight. Blah,blah. [John Smith the contractor shall blah. Blah blah]
it would process Blah, blah. Mr. [Smith the contractor shall blah. Blah, blah.

because Word confuses the period after Mr. as a sentence stop.

No (1) is not one paragraph to the right. It is the first paragraph in the ranges paragraph collection. So when the range is defined as a word or snippet in a paragraph the range.Paragraphs(1) is that ranges parent paragraph.

Collapse 0 is simple short for Collapse wdCollapseEnd where wdCollapseEnd is constant with the value = 0
You collapse the range so you don't get stuck in a loop finding the same thing over and over again.

lbl_Exit:
Exit Sub

Is just my style that my good friend Graham liked and adapted.
It doesn't serve much purpose other that to tell me that I have at least thought more than absently about what the code does and serves as an exit loop:

On Error GoTo lbl_Err
Err.Raise 6
lbl_Exit:
Exit Sub
lbl_Err:
Msgbox Err.Number & " " Err.Description
Resume lbl_Exit
End Sub