Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 05-26-2012, 01:55 AM
Chayes Chayes is offline Find and highlight multiple words in MS Word document Windows XP Find and highlight multiple words in MS Word document Office 2003
Advanced Beginner
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Smile

Hi



Yes , highlighting the whole sentence , or just one or two words or numbers either side of the target text would be fine.

All of my sentences start with a number so wouldn't ever fall foul of the found words starting the sentence. In my case I'm highlighting certain words in a large catalogue , and need the highlighting to be wider than the single found word.

I'll attach a small example so you can see more precisely what the context involves. If you could suggest some code to do the trick that would be great.

Thanks Again. Much appreciated.




Attached Files
File Type: doc Auction Sample.doc (35.0 KB, 15 views)
Reply With Quote
  #17  
Old 05-26-2012, 06:11 PM
macropod's Avatar
macropod macropod is offline Find and highlight multiple words in MS Word document Windows 7 64bit Find and highlight multiple words in MS Word document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Chayes,

Since your list actually has a separate paragraph for each entry, there possibly aren't any sentence or line issues to worry about. One can simply highlight the whole paragraph. You can do that with the following version of the macro:
Code:
Sub HiLightList()
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long, j As Long
StrFnd = UCase("jewellery|ring|rings|napkin rings|watch|watches")
For i = 0 To UBound(Split(StrFnd, "|"))
  Select Case i Mod 6
    Case 0: j = 3
    Case 1: j = 4
    Case 2: j = 5
    Case 3: j = 6
    Case 4: j = 7
    Case 5: j = 16
  End Select
  Options.DefaultHighlightColorIndex = j
  Set Rng = ActiveDocument.Range
  With Rng.Find
    .ClearFormatting
    .Text = "[!^13]@<" & Split(StrFnd, "|")(i) & ">[!^13]{1,}"
    .Replacement.ClearFormatting
    .Replacement.Highlight = True
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Some things to note:
• the code highlights the different entries in of of six colours. This is managed via the Select Case ... Options.DefaultHighlightColorIndex = j lines. Although Word can apply highlighting in any of 16 colours, I didn't think you'd find black, white or dark highlights very useful, so only 6 are used. If you're not interested in having different colours, simply delete those lines.
• given the different shadings, if you want to shade 'gold watch' differently from 'watch' (ie where the material isn't defined), you need to have the 'gold watch' entry appear after the 'watch' entry in the StrFnd list.
• the macro uses a wildcard Find/Replace and looks for whole words. Thus, if you want to highlight entries with watch and watches, both need to be entered into the StrFnd list. Alternatively, if you wanted to have both singular and plural forms of a word (eg 'watch' and 'watches') given the same higlighting, replace the last character from the singular form with '[! ]@'. Thus 'watch' becomes 'watc[! ]@'.
• it doesn't matter whether you input entries into the StrFnd list in upper or lower case - everything is converted to upper-case for the Find operation.
• you must have a '|' character separating each entry in the StrFnd list.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #18  
Old 05-27-2012, 06:13 AM
Chayes Chayes is offline Find and highlight multiple words in MS Word document Windows XP Find and highlight multiple words in MS Word document Office 2003
Advanced Beginner
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Default

HI

Ok Thanks for this. I'm grateful for your help.

I find it works really well when I run it on the original text. It finds the entries in the StrFnd list.

Curiously though it misses entries which are added subsequently. For example , if i open the document and run the macro it highlights all correctly. If I then type in an item of target text to see if it would find that too , it doesn't.

To experiment further , I saved the document with the newly-added text included. On re-opening , I find the macro doesn't find the new text. I am at a loss to explain this.

I did make some changes to the StrFnd list to search for the precise terms I'm looking to highlight , of course , but otherwise I'm running it as shown :
Code:
Sub HiLightList()
'This macro finds and highlights all the words in the StrFnd list below
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long, j As Long
StrFnd = UCase("LP|LPs|Vinyl|Record|Records|CD|CDs|DVD|DVDs|Music|Programme|Programmes|Cassette|Cassettes|Tape|Tapes|45s|78s|78 rpm|78rpm")
For i = 0 To UBound(Split(StrFnd, "|"))
  Select Case i Mod 6
    Case 0: j = 3
    Case 1: j = 4
    Case 2: j = 5
    Case 3: j = 6
    Case 4: j = 7
    Case 5: j = 16
  End Select
  Options.DefaultHighlightColorIndex = j
  'This line specifies colour for all matches
  Options.DefaultHighlightColorIndex = wdPink
  Set Rng = ActiveDocument.Range
  With Rng.Find
    .ClearFormatting
    .Text = "[!^13]@<" & Split(StrFnd, "|")(i) & ">[!^13]{1,}"
    .Replacement.ClearFormatting
    .Replacement.Highlight = True
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub


I can't think that these small changes would cause it to ignore some of the entries to the list.

I'll attach again a fuller version of my target text , I'd be intersted to see if you can create the same phenomen.

Thanks again.
Attached Files
File Type: doc AucReady.doc (79.0 KB, 13 views)

Last edited by macropod; 05-27-2012 at 03:00 PM. Reason: Added code tags & formatting
Reply With Quote
  #19  
Old 05-27-2012, 03:08 PM
macropod's Avatar
macropod macropod is offline Find and highlight multiple words in MS Word document Windows 7 64bit Find and highlight multiple words in MS Word document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Chayes,

The macro works fine for me when I add new items. One thing to note is that the macro expects there to be at least a space each side of the words being searched for. Thus, it won't find a line that says just 'RECORD' or 'RECORD ' or ' RECORD'.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #20  
Old 05-27-2012, 04:11 PM
Chayes Chayes is offline Find and highlight multiple words in MS Word document Windows XP Find and highlight multiple words in MS Word document Office 2003
Advanced Beginner
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Smile

Ok Yes , I see that now.

I'm adding sample phrases with spaces either side now and it's finding them fine. Many thanks for this - it's a very neat and useful script.

Reply With Quote
  #21  
Old 06-08-2013, 12:31 PM
Fgrable Fgrable is offline Find and highlight multiple words in MS Word document Windows 7 64bit Find and highlight multiple words in MS Word document Office 2010 64bit
Novice
 
Join Date: Jun 2013
Location: Chicago
Posts: 1
Fgrable is on a distinguished road
Smile Had some problems with one macro suggested

I, too, had a need for finding multiple words in a word document and ran across this thread. I find that the initial macro offered works well for multiple single words. In the thread, it was suggested that the macro could find phrases by simply putting the phrase in-between the commas:
Quote:
for a multi-word string as part of the Find array, you simply input that between the commas. For example:
StrFnd = "dog,cat,pig,this and that,horse,man"
However, when I attempted to run my macro on the document with a multi-word string, it failed. Here's my macro which mirrors what was suggested early on in this thread (but using some words and phrases that I am trying to find):
Code:
Sub HiLightList()
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long
StrFnd = "portable,residential,aquatic vessel,barrier,listed,labeled,flood hazard area,self-contained spa"
For i = 0 To UBound(Split(StrFnd, ","))
  Set Rng = ActiveDocument.Range
  With Rng.Find
    .ClearFormatting
    .Text = Split(StrFnd, ",")(i)
    .Replacement.ClearFormatting
    .Replacement.Highlight = True
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = True
    .Execute Replace:=wdReplaceAll
  End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Through intuition and dinking around, I found that if I changed

.MatchAllWordForms = False


The macro runs and highlights the exact words and phrases that I asked it to find even those words or phrases have the plural "s" at the end. In other words, it will find and highlight only "barrier" in the word "barriers".

And for a first pass, that is OK for my purposes because I just want to only locate the word or word phrase so I can make manual document changes, if I feel the change is needed.

Questions:

1. I have a few phrases that contain the word "and". For example "hair and lint strainer". The macro will run, but it won't find the phrase. Any SIMPLE suggested modification to make the macro find this?

2. In searching for a solution to the problem I was having, I ran across someone who was talking about putting the words in an excel sheet (one column) and being able to make a Word macro go look to the excel document, pull the words from it (and I assume put them in comma delimited text format) and then use that for running the macro. I don't necessarily want to use Excel (but i could) but the idea made me think that instead of having to "rewrite" the macro for different word lists, I could have a generic "finder" macro that go to a specific file to get the words. That way, I could just have a simple word (or excel) file to maintain and run the macro on my documents using the words in that word (or excel) file. Any thoughts on this or would that take rocket scientists to work out?

2. This macro writing for Word is a nifty treat for oddball document manipulation. I am intrigued. How do I find out more about how to do this type of programming? It reminds me of AutoLisp in the AutoCad programs...rarely does anybody know of its capability but how powerful it can be for working on hundreds of projects.

PS: Thank you Macropod for initially sharing your expertise on this subject. This macro is going to be very useful for me and my co-workers. Perhaps as I learn more, I'll come back with some more questions and ideas (hopefully new tricks I have learned!)

Fred

Last edited by macropod; 06-09-2013 at 04:19 AM. Reason: Added code tags & formatting
Reply With Quote
  #22  
Old 06-09-2013, 04:30 AM
macropod's Avatar
macropod macropod is offline Find and highlight multiple words in MS Word document Windows 7 32bit Find and highlight multiple words in MS Word document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

1. If you want to find "hair and lint strainer", simply use that in the find expression. Using "and' on its own will only find that - not the rest of the phrases containing it.

2. I have posted other macros here that do indeed use Excel workbooks as the Find/Replace source. See, for example: https://www.msofficeforums.com/word-...d-replace.html

3. There are many books on Word automation and on vba programming. Another good way of learning is to study other people's code (which account for much of my own learning).

Note: I'm not going to be able to respond further, as I'm going off-line for an extended period.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #23  
Old 01-28-2016, 07:28 PM
phil in orlando phil in orlando is offline Find and highlight multiple words in MS Word document Windows 10 Find and highlight multiple words in MS Word document Office 2010 64bit
Novice
 
Join Date: Jan 2016
Posts: 1
phil in orlando is on a distinguished road
Default

@macropod
Dude, you saved me hours of work by using your macro to find and highlight multiple words in a word document.
Thank you, thank you, thank you!!!
Phil in Orlando
Reply With Quote
  #24  
Old 02-02-2016, 04:31 PM
rok123 rok123 is offline Find and highlight multiple words in MS Word document Windows 7 64bit Find and highlight multiple words in MS Word document Office 2013
Novice
 
Join Date: Jan 2016
Posts: 8
rok123 is on a distinguished road
Default

Hi,
How can I search multiple phrases. It seems this macro searches multiple words.

Thanks!
Reply With Quote
  #25  
Old 02-02-2016, 04:38 PM
macropod's Avatar
macropod macropod is offline Find and highlight multiple words in MS Word document Windows 7 64bit Find and highlight multiple words in MS Word document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The code works equally well with single words and phrases.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #26  
Old 08-19-2017, 10:55 AM
jaidinesh jaidinesh is offline Find and highlight multiple words in MS Word document Windows 7 32bit Find and highlight multiple words in MS Word document Office 2013
Novice
 
Join Date: Aug 2017
Posts: 3
jaidinesh is on a distinguished road
Default how can we work with this same code for spaced words like "that is"

How can we work with this code for spaced words like "that is"
Reply With Quote
  #27  
Old 08-19-2017, 01:57 PM
macropod's Avatar
macropod macropod is offline Find and highlight multiple words in MS Word document Windows 7 64bit Find and highlight multiple words in MS Word document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Simply delete the '.MatchAllWordForms = True' line.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #28  
Old 08-20-2017, 07:42 AM
jaidinesh jaidinesh is offline Find and highlight multiple words in MS Word document Windows 7 32bit Find and highlight multiple words in MS Word document Office 2013
Novice
 
Join Date: Aug 2017
Posts: 3
jaidinesh is on a distinguished road
Default what about spaced words like 'the cat, the dog house, Jr. John'

Hi Paul,

How can we find multiple spaced words like 'the cat, the dog house, Jr. John', can you give the code with this examples?
Reply With Quote
  #29  
Old 08-20-2017, 07:46 AM
jaidinesh jaidinesh is offline Find and highlight multiple words in MS Word document Windows 7 32bit Find and highlight multiple words in MS Word document Office 2013
Novice
 
Join Date: Aug 2017
Posts: 3
jaidinesh is on a distinguished road
Default how can we work with punctuation likes "i.e.,e.g.,teacher's"

Hi Paul,

It works for spaced words, thanks a lot.

How can we work with punctuation likes "i.e.,e.g.,teacher's"
Reply With Quote
  #30  
Old 08-20-2017, 02:15 PM
macropod's Avatar
macropod macropod is offline Find and highlight multiple words in MS Word document Windows 7 64bit Find and highlight multiple words in MS Word document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

By changing:
.MatchAllWordForms = True
to:
.MatchAllWordForms = False
the code already works for any punctuation except a comma. If you need to work with commas as well, use:
Code:
Sub HiLightList()
Application.ScreenUpdating = False
Dim StrFnd As String, h As Long, i As Long
h = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdYellow
StrFnd = "dog|cat|pig|horse|man"
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Replacement.Highlight = True
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .Font.Underline = False
  .MatchCase = True
  .MatchWholeWord = True
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Replacement.Text = "^&"
  For i = 0 To UBound(Split(StrFnd, "|"))
    .Text = Split(StrFnd, "|")(i)
    .Execute Replace:=wdReplaceAll
  Next
End With
Options.DefaultHighlightColorIndex = h
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
find, highlight, multiple keywords

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and highlight multiple words in MS Word document Lost Word Document and cant find it!!! I saved it but it ISNT there!! APAV Word 9 10-09-2017 01:17 PM
Highlight text and find next instance DrDOS Word 0 11-15-2010 04:02 PM
Lock words in a document, but allow for input within the document tlinde Word 1 02-09-2010 09:07 PM
FInd recurring words in Word 2003 NJ007 Word 4 01-25-2010 03:11 PM
find - reading highlight - highlight all / highlight doesn't stick when saved bobk544 Word 3 04-15-2009 03:31 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:11 PM.


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