#1
|
|||
|
|||
Macro to highlight words
My job involves checking for definitions in legal documents for clients. I want to ignore certain capitalised terms that never need defining (countries, months etc). I'd like to build up a list of these terms and then at the click of button highlight them throughout a document, so I'm left only checking relevant terms. How do I do this?
This is a big thing - it's what I do about 30 hours a week - so I'd really appreciate any help (bearing in mind that I've never used macros or VBA before and have limited understanding of how they work or whether they are what is needed). Thanks Bertie |
#2
|
|||
|
|||
Here is some code that I use to help me:
Sub ListChange() Dim r As Range Dim MyList() As String Dim i As Long MyList = Split("dot,com,like", ",") For i = 0 To UBound(MyList()) Set r = ActiveDocument.Range With r.Find .Text = MyList(i) .Replacement.Highlight = wdYellow .Execute Replace:=wdReplaceAll End With Next End Sub |
#3
|
|||
|
|||
I also found this code on the Web.
I also have a macro for highlighting lists of Words which could be adapted to your own requirements. Replace the words in the vFindText arrays with your own words. You can continue the theme with other colours and words as required. Sub ReplaceList() Dim vFindText As Variant Dim vReplText As Variant Dim i As Long 'highlight red words Options.DefaultHighlightColorIndex = wdRed Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting vFindText = Array("anger", "violence", "fighting") vReplText = "^&" With Selection.Find .Forward = True .Wrap = wdFindContinue .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Format = True .MatchCase = False For i = LBound(vFindText) To UBound(vFindText) .Text = vFindText(i) .Replacement.Text = vReplText .Replacement.Highlight = True .Execute replace:=wdReplaceAll Next i End With 'Highlight blue words Options.DefaultHighlightColorIndex = wdBlue vFindText = Array("depression", "misery") vReplText = "^&" With Selection.Find .Forward = True .Wrap = wdFindContinue For i = LBound(vFindText) To UBound(vFindText) .Text = vFindText(i) .Replacement.Text = vReplText .Replacement.Highlight = True .Execute replace:=wdReplaceAll Next i End With End Sub |
#4
|
|||
|
|||
Much appreciated, Big0. I've taken Monday off so I can have a look at this, try and make sense of how to enter it - I know nothing about codes - and give it a shot.
|
#5
|
|||
|
|||
Open you document
Click ALT-F11 Go to INSERT Click on Module Paste Code Save it Close it Should take you back to your document, if not reopen Click VIEW Click MACRO Your MACRO name should come up and then click RUN Should work big0 |
#6
|
|||
|
|||
Thanks for the instructions, Big0. Monday could be a day of celebration! I'd do it now but I'm on a work bender for the next few days.
|
#7
|
|||
|
|||
Hi Big0
OK, I tried it, replacing your Array with the following line: vFindText = Array("Schedule", "English", "The", "United States", "Scheduled Completion Date") It replaced 'Schedule' with red but not the other words - any idea? Anyway, this is definitely on the right tracks so I'm very grateful. I need to develop this further so that I can quickly locate standalone terms and multi-word terms, so that, eg, I can highlight: '[not preceded by an upper-case word with certain exceptions, eg 'The'] Issuer [not followed by any upper-case word]', so that '[The/Each etc] Issuer' is highlighted but not 'Subordinated Issuer', 'Issuer Event of Default' etc. or highlight multi-words so, eg, highlight instances of 'Subordinated Issuer' but not the 2 words separately. And to do this for hundreds of terms in each document! (Down the line, the elusive goal would be to auto-locate and list bold definitions in a document and auto-list terms that are not used except where defined - probably not achievable; otherwise the program would be out there, but as near as dammit would suit me) The programming skills would, I imagine, be quite advanced to achieve what I want, but do you think this can be developed further? And if so, what's the most cost-effective way (obviously, I wouldn't expect anyone to do this for free) -I was thinking of contacting a university department. Or do you have the skills? PM me if maybe so. Thanks Bertie Last edited by bertietheblue; 07-01-2013 at 05:59 AM. |
#8
|
|||
|
|||
I should say at the moment I'm jut using wildcards, specifically (eg Issuer):
"[A-Z][a-z]{3,} Issuer" [not highlight] - to see if preceded by u/c word (having already highlighted all u/c words at beginning of paras (by highlighting wildcard 13^[A-Z]) and sentences (by highlighting all instances of '. ^$' and '. ^$') and "Issuer [A-Z]" to see if followed by u/c word. Time-consuming! |
#9
|
|||
|
|||
Look through each of the vFindText in the code and add your word that you want highlighted. I see TWO vFindText CODES. One is Red and the other is blue, if you need additional colors. Then you can copy the start of code and the end of code and change to the next color. See below:
'highlight yellow words Options.DefaultHighlightColorIndex = wdYellow Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting vFindText = Array("Teacher", "Students", "Grades") vReplText = "^&" With Selection.Find .Forward = True .Wrap = wdFindContinue .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Format = True .MatchCase = False For i = LBound(vFindText) To UBound(vFindText) .Text = vFindText(i) .Replacement.Text = vReplText .Replacement.Highlight = True .Execute replace:=wdReplaceAll Next i End With Then copy and paste additional colors into the code below: Make sure you change From this: 'highlight yellow words Options.DefaultHighlightColorIndex = wdYellow To this 'highlight green words Options.DefaultHighlightColorIndex = wdGreen and add vFindText = Array("Next word", "Next word", "Next word") |
#10
|
|||
|
|||
If you want post your document and let me look at it and let me know the specifics you want added and see what I can do for you.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Find and highlight multiple words in MS Word document | AtaLoss | Word VBA | 37 | 09-22-2021 12:04 PM |
Macro to highlight a list of words | bakerkr | Word VBA | 4 | 10-19-2017 02:23 PM |
How to make it highlight blocks of text (words) without highlighting extra space @end | seortm | Word | 3 | 03-30-2015 08:12 AM |
Highlight and then replace multiple words | redhin | Word VBA | 5 | 03-05-2013 05:42 AM |
Find and highlight all words ending in -ly | RBLampert | Word VBA | 13 | 10-23-2012 04:45 PM |