#16
|
|||
|
|||
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. |
#17
|
||||
|
||||
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 • 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] |
#18
|
|||
|
|||
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. Last edited by macropod; 05-27-2012 at 03:00 PM. Reason: Added code tags & formatting |
#19
|
||||
|
||||
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] |
#20
|
|||
|
|||
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. |
#21
|
|||
|
|||
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:
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 .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 |
#22
|
||||
|
||||
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] |
#23
|
|||
|
|||
@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 |
#24
|
|||
|
|||
Hi,
How can I search multiple phrases. It seems this macro searches multiple words. Thanks! |
#25
|
||||
|
||||
The code works equally well with single words and phrases.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#26
|
|||
|
|||
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"
|
#27
|
||||
|
||||
Simply delete the '.MatchAllWordForms = True' line.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#28
|
|||
|
|||
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? |
#29
|
|||
|
|||
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" |
#30
|
||||
|
||||
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] |
Tags |
find, highlight, multiple keywords |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |