![]() |
#1
|
|||
|
|||
![]()
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. |
#2
|
||||
|
||||
![]()
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
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)? |
#4
|
||||
|
||||
![]()
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] |
#5
|
|||
|
|||
![]()
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?
|
#6
|
||||
|
||||
![]()
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] |
#7
|
|||
|
|||
![]()
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. |
#8
|
|||
|
|||
![]()
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 |
#9
|
||||
|
||||
![]() Quote:
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 |
#10
|
|||
|
|||
![]()
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? |
#11
|
||||
|
||||
![]()
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] |
#12
|
|||
|
|||
![]()
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 |
#13
|
||||
|
||||
![]()
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] |
#14
|
|||
|
|||
![]()
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. |
#15
|
||||
|
||||
![]()
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] |
![]() |
Tags |
find, highlight, multiple keywords |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |