Microsoft Office Forums Find Word then Highlight Whole Sentence and Paragraph Around it

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-07-2016, 09:32 AM
ChrisOK ChrisOK is offline Find Word then Highlight Whole Sentence and Paragraph Around it Windows 7 64bit Find Word then Highlight Whole Sentence and Paragraph Around it Office 2016
Advanced Beginner
Find Word then Highlight Whole Sentence and Paragraph Around it
 
Join Date: Sep 2016
Posts: 34
ChrisOK is on a distinguished road
Question Find Word then Highlight Whole Sentence and Paragraph Around it

The code needs to locate 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!

Not sure if that needs to be 2 separate chunks of code or just a matter of changing a couple of things to make it go from only doing a SENTENCE vs. doing a PARAGRAPH.

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?
Selection.StartOf Unit:=wdParagraphm
Selection.MoveEnd Unit:=wdParagraph

Here's the code 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 start with something better!




Code:
 
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
Attached Files
File Type: docm CONTRACTOR SHALL are the key words.docm (18.5 KB, 1 views)
Reply With Quote
  #2  
Old 09-08-2016, 01:04 AM
gmayor's Avatar
gmayor gmayor is offline Find Word then Highlight Whole Sentence and Paragraph Around it Windows 10 Find Word then Highlight Whole Sentence and Paragraph Around it Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,939
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

Asked and answered at http://www.vbaexpress.com/forum/show...ound-That-Word

Please do not post to multiple forums without cross posting correctly.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 09-08-2016, 04:00 AM
macropod's Avatar
macropod macropod is offline Find Word then Highlight Whole Sentence and Paragraph Around it Windows 7 64bit Find Word then Highlight Whole Sentence and Paragraph Around it Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,569
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Also cross-posted and answered at: http://www.office-forums.com/threads...llows.2349709/
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #4  
Old 09-08-2016, 08:16 PM
ChrisOK ChrisOK is offline Find Word then Highlight Whole Sentence and Paragraph Around it Windows 7 64bit Find Word then Highlight Whole Sentence and Paragraph Around it Office 2016
Advanced Beginner
Find Word then Highlight Whole Sentence and Paragraph Around it
 
Join Date: Sep 2016
Posts: 34
ChrisOK is on a distinguished road
Thumbs up Solutions

Thanks for the help and the cross-post etiquette link- I read it and have done/will continue to my best to make sure no one is left hanging. (that's one thing that's so frustrating - coming across a post that's EXACTLY what you need an answer to but no one ever posted a solution.. Either (a) the poor soul just gave up or (b) found an answer elsewhere and never came back to share it.. We'll never know which - but for anyone who comes across this one, I've got at least 2 solutions that newbies like me can review, (hopefully learn from) and use as they wish to help them too! It never ceases to amaze me how there's so many diff ways to achieve the same outcome.. interesting to read each line to see how the methods differ... (well, at least it is to me as someone who's still learning)


Method 1 Code (MatchWildcards=False):

Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "Contractor Shall"
  .Replacement.Text = ""
  .Forward = True
  .Wrap = wdFindStop
  .Format = True
  .MatchWildcards = False
  .Execute
  End With
  Do While .Find.Found
  .Duplicate.Paragraphs.First.Range.HighlightColorIndex = wdYellow
  .Start = .Duplicate.Paragraphs.First.Range.End
  .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

Method 2 Code (Alternative):
Code:
Sub Highlight_Paragraph()
  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
Reply With Quote
  #5  
Old 09-08-2016, 10:16 PM
macropod's Avatar
macropod macropod is offline Find Word then Highlight Whole Sentence and Paragraph Around it Windows 7 64bit Find Word then Highlight Whole Sentence and Paragraph Around it Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,569
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Yet another answered cross-post found at: http://windowssecrets.com/forums/sho...h-that-Follows

Kindly notify ALL boards on which you've cross-posted that you have multiple solutions, providing links between them to all the other threads in which you've cross-posted.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Tags
find word highlight para

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find Word then Highlight Whole Sentence and Paragraph Around it Find and highlight multiple words in MS Word document AtaLoss Word VBA 33 10-10-2017 01:35 AM
Find Word then Highlight Whole Sentence and Paragraph Around it Macro Question: Need help making a macro to highlight the first word in every sentence LadyAna Word 1 12-06-2014 10:39 PM
Find Word then Highlight Whole Sentence and Paragraph Around it Word thinks 1 sentence is 2 paragraphs and won't let me delete the extra paragraph symbol jrasicmark Word 1 12-05-2014 01:50 AM
Find/Replace, Duplicating a word in a sentence to hundreds of sentences. DDDD Word 0 10-08-2013 10:48 AM
find - reading highlight - highlight all / highlight doesn't stick when saved bobk544 Word 3 04-15-2009 03:31 PM


All times are GMT -7. The time now is 06:14 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft