![]() |
|
|
|
#1
|
||||
|
||||
|
Since you can't us a BackgroundPatternColor property as a Find parameter, you'll need to revert to a nested loop. Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, arrWords, i As Long, RGB As Long
arrWords = Array("keyword1 & keyword2", "keyword1", "keyword2", "keyword3")
Set Rng = Selection.Range
Rng.Paste
For i = 0 To UBound(arrWords)
Select Case i
Case 0: RGB = wdColorGray05
Case 1: RGB = wdColorPaleBlue
Case 2: RGB = wdColorLightOrange
Case 3: RGB = wdColorPink
End Select
With Selection
With .Find
.ClearFormatting
.Text = arrWords(i)
.Replacement.Text = ""
.Format = False
.Forward = True
.Wrap = wdFindStop
End With
Do While .Find.Execute
If .InRange(Rng) = False Then Exit Do
If .Shading.BackgroundPatternColor = wdColorAutomatic Then .Shading.BackgroundPatternColor = RGB
.Collapse wdCollapseEnd
Loop
End With
Rng.Select
Next
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#2
|
|||
|
|||
|
Brilliant, thank you!
One more thing, and I'd like to point out that I'm asking this mostly because I know there will be other people that will find this thread and may use your code for themselves. Today I spend about half an hour searching Google for Word highlighters, and while I found quite many, almost no one had the ability to use RGB colors. Except for Greg's Highlighter, they all used the 'HighlightColorIndex' property. As you wrote it, I understand that each keyword in the 'arrWords' array has a corresponding color in the 'Case 0,1,2,etc' RGB list. So there's a keyword/color pair, and I hope I'm not mistaken. Please let me know if I am. Like me, I think most people who use such highlighters need to assign a single color to an entire list of keywords. Basically, you want ten keywords to be highlighted in red, 15 keywords in blue, and so on. With this in mind, I think the best approach would be to have multiple arrays, each one with a corresponding color. Something like this: arrWords1 = Array("keyword1", "keyword2") arrWords2 = Array("keyword1", "keyword2", "keyword3") arrWords3 = Array("keyword1", "keyword2", "keyword3", "keyword4", "keyword5", "keyword6") Then, the RGB list should basically assign one color to each array: arrWords1: RGB (10, 175, 150) arrWords2: RGB (74, 18, 188) arrWords3: RGB (150, 175, 190) What do you think? This is what I am looking for, and I'm sure this solution would be the closest one to most user's needs. Most probably, this is just a minor change to your code, but with lots of added value. Alex |
|
#3
|
||||
|
||||
|
Quote:
Quote:
Quote:
• 15 blue, 15 red, and so on, then padding out the arrWords array with empty elements for keyword groups that have less than 15 items; or • only the required number of colour definitions to match what's in the arrWords array, either of which could be used in conjunction with specifying a range of values for 'i' in the Select Case statement. For example: Code:
Select Case i
Case 0 - 14: RGB = wdColorRed
Case 15 - 29: RGB = wdColorOrange
Case 30 - 44: RGB = wdColorYellow
Case 44 - 59: RGB = wdColorGreen
Case 60 - 74: RGB = wdColorBlue
End Select
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| I am trying to delete text of any format between two bold, recurring keywords or symbols | qubie | Word VBA | 8 | 03-04-2020 03:20 PM |
how do I make pasted text the same size as the text it's being pasted into?
|
David Lee | Word | 6 | 08-16-2015 10:46 AM |
Help with finding multiple keywords in a single document then highlight
|
navyguy | Word | 2 | 01-03-2014 12:48 PM |
| Trying to highlight pasted text in a macro | goldengate | Word VBA | 0 | 09-14-2010 09:41 PM |
| find - reading highlight - highlight all / highlight doesn't stick when saved | bobk544 | Word | 3 | 04-15-2009 03:31 PM |