Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-18-2011, 03:27 PM
AtaLoss AtaLoss is offline Find and highlight multiple words in MS Word document Windows 2K Find and highlight multiple words in MS Word document Office 2003
Novice
Find and highlight multiple words in MS Word document
 
Join Date: May 2011
Posts: 3
AtaLoss is on a distinguished road
Unhappy Find and highlight multiple words in MS Word document

Hello all,

Is it possible in MS Word 2003 (or in later versions?) to run the 'find and replace' function with not only one single word, but a number of words?

What I want to do is highlight in my word doc about 10 different words, and I'm wondering if there's a better way than running 10 separate 'find/replace' queries. I'm going to need to do this on a large number of word docs, so doing one 'find/replace' rather than 10 for each doc would considerably shorten the whole operation.

A friend sent me a macro (http://help.lockergnome.com/office/W...ct1010011.html) that enabled me to highlight all occurences of my 10 keywords at once, but I have two issues with this solution:
- two of my keywords are 'man' and 'men', and so all words that include these three letters get highlighted.
- (if I got that right) the macro would apparently need to be manually installed in every single one of my many, many word docs, so that would also be a big waste of time.

Thanks in advance for your help.
Reply With Quote
  #2  
Old 05-18-2011, 06:15 PM
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 2007
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 AtaLoss,

Try something based on:
Code:
Sub HiLightList()
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long
StrFnd = "dog,cat,pig,horse,man"
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
Note: Because I've set 'MatchAllWordForms = True', a Find for 'cat' or 'man' will also find & highlight 'cats' & 'men'. Apart from that, the matched words must be whole words. That means you don't need multiple expressions to capture the various forms of a given word.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-19-2011, 07:36 AM
AtaLoss AtaLoss is offline Find and highlight multiple words in MS Word document Windows 2K Find and highlight multiple words in MS Word document Office 2003
Novice
Find and highlight multiple words in MS Word document
 
Join Date: May 2011
Posts: 3
AtaLoss is on a distinguished road
Default Thank you!

Thanks a million - it works perfectly and will save me so much time. Thanks thanks thanks!

Quick follow-up question - there is no way to make that macro work automatically in every word doc, is there? I have a couple hundred word docs to do this keyword search in - do I need for each doc to go to macro, insert module, copy and paste the code, or is there any quicker way (other than me copying/pasting all these docs into a single one)?
Reply With Quote
  #4  
Old 05-19-2011, 03:14 PM
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 2007
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 AtaLoss,

If you add the code to Word's 'Normal' template instead of adding it to a document, it will be available for use in all documents.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-26-2011, 12:28 PM
AtaLoss AtaLoss is offline Find and highlight multiple words in MS Word document Windows 2K Find and highlight multiple words in MS Word document Office 2003
Novice
Find and highlight multiple words in MS Word document
 
Join Date: May 2011
Posts: 3
AtaLoss is on a distinguished road
Default How about apostrophes?

I've been using the macro (applied it to 'normal', thanks!), but I just realized that there's one word form it doesn't recognize/take into account: words with an apostrophe at the end. For instance, it recognizes "women" and "women.", but not "women's". I tried adding to the list of keywords "women'", but got an error message. Any idea what I could do?
Reply With Quote
  #6  
Old 05-26-2011, 03:34 PM
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 2007
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 AtaLoss,

That's a limitation of using 'MatchAllWordForms = True' - it only accepts letters. One way to handle this would be to force the Find/Replace to use both 'MatchAllWordForms = True' and 'MatchAllWordForms = False'. For example, add:
On Error Resume Next
before:
For i = 0 To UBound(Split(StrFnd, ","))
and insert:
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
after:
.Execute Replace:=wdReplaceAll
This will pick up the words with apostrophes, but not the apostrophe and whatever follows it - unless you put the apostrophe and whatever follows it into the array.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 12-01-2011, 08:10 AM
jovani jovani is offline Find and highlight multiple words in MS Word document Windows XP Find and highlight multiple words in MS Word document Office 2007
Novice
 
Join Date: Dec 2011
Posts: 3
jovani is on a distinguished road
Default

I love that script MacroPod.
Is there a way to tweak it to highlight the different words being searched for in different colors. Doesn't matter which color.
So as to help spotting two different words on the same page?

I forgot to ask this other question, There's also this issue: If one of my search words are 'biological' and there exists a word in the document 'biologically' the script passes up that occurrence.
Any ideas to fix that?
I'm new to this but learning as I go.
Reply With Quote
  #8  
Old 12-01-2011, 05:52 PM
jovani jovani is offline Find and highlight multiple words in MS Word document Windows XP Find and highlight multiple words in MS Word document Office 2007
Novice
 
Join Date: Dec 2011
Posts: 3
jovani is on a distinguished road
Default Hybrid

Just wondering if I can integrate this script as well. I thought it would be interesting to be able to drop this in to have it find, well just as it says:

the reason the script is in there twice is so that it can find the two words no mater which one comes first (random order) But a hybrid of the script already made and this would be interesting. I will be trying to piece this together.

Two Words Near Each Other in a Any Order

\b(?:word1\W+(?:\w+\W+){1,6}word2|word2\W+(?:\w+\W +){1,6}word1)\b
Reply With Quote
  #9  
Old 12-01-2011, 09:44 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

Quote:
Originally Posted by jovani View Post
I love that script MacroPod.
Is there a way to tweak it to highlight the different words being searched for in different colors. Doesn't matter which color.
So as to help spotting two different words on the same page?
You could do that, by inserting after:
For i = 0 To UBound(Split(StrFnd, ","))
then line:
Options.DefaultHighlightColorIndex = i Mod 16 + 1
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 12-02-2011 at 04:10 AM. Reason: Added Mod function as ColorIndex only goes to 15
Reply With Quote
  #10  
Old 12-01-2011, 11:06 PM
jovani jovani is offline Find and highlight multiple words in MS Word document Windows XP Find and highlight multiple words in MS Word document Office 2007
Novice
 
Join Date: Dec 2011
Posts: 3
jovani is on a distinguished road
Default

Ok so then I would have not only arguements in the multisearch and then two more words to type in for this new line of code? How does that work?
Also this new line of code has two mentions of word1 and word2 I'm guessing the two real arguements up against the middle pipe character right?
So for instance if I wanna find two words: fight & bow.
I would type the two criteria once for the criteria above and then type the two variables again for this new line of code that you're telling me I can drop in? Right?
Reply With Quote
  #11  
Old 12-03-2011, 01:33 AM
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

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"
If you need to find strings that include commas, you'd need to change the StrFind expression's separators and the Split expression's separators. For example:
StrFnd = "dog|cat|pig|this, that or something else|horse|man"
...
For i = 0 To UBound(Split(StrFnd, "|"))
...
.Text = Split(StrFnd, "|")(i)

As for finding two words that are near each other, you'd need to use a wildcard Find (ie .MatchWildcards = True) and, in the Find statement, an expression like:
"word1[?]{1,10}word2"
where 1 represents the minimum permitted number of intervening characters and 10 represents the maximum permitted number of intervening characters.

Do note that wildcard Find expressions are case-sensitive so, if a word might be in, say, title case, you'd need something like:
"[Ww]ord1 [?]{1,10} [Ww]ord2"
As for reversing the word order, that would require two Find operations.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 05-25-2012, 02:59 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
Default

Hi Macropod ,

I used your script to highlight a list of words in a document.

Presently it highlights the single words in the list.

I was wondering if it could be adapted to highlight the whole line of text in which each keyword appears. Can you advise?



Best Wishes
Reply With Quote
  #13  
Old 05-25-2012, 06:26 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 in this thread would need considerable re-working before being able to highlight a whole line. Moreover 'line' is a fluid concept in Word, as what fits on a line depends on many variables, including the page layout, margins, font, point size and the current printer driver. Simply changing the printer driver (and even some of its settings) can affect what fits on a line. So, if you highlight a 'line' on your PC, then send the document to someone else, the 'line' may now occupy part of two different lines. Much easier, though, if your 'line' is a complete paragraph.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old 05-25-2012, 07: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

HI

Ok Thanks for getting back.

Point taken. I appreciate it's a moveable feast as such. Nevertheless , I was hoping that the code might be able to accommodate the circumstances that it finds , whatever they may be.

In my case I have a fixed use where the lines all have a certain width with no overlapping. It wouldn't really matter if there were some inconsistency in the output.

I was wondering also if it would be possible in the absence of a sensible 'whole line' solution if the width of the highlight could be extended to a few characters or words either side of the target text. Is that a more feasible proposition?

Grateful for your time and expertise.
Reply With Quote
  #15  
Old 05-25-2012, 07:30 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,

Yes, it's certainly possible the highlight a few words either side, though working with, say, a whole sentence might be better. That overcomes issues where the found words start a sentence or paragraph and whatever you might otherwise highlight either side of them is unrelated. Of course, highlighting a whole sentence might be more than you want. Additional processing could be added to constrain the highlighted range to a few words either side within the same sentence.
__________________
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 01:39 AM.


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