View Single Post
 
Old 05-26-2012, 06:11 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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,

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
Some things to note:
• 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]
Reply With Quote