Alan,
I monkeyed around with this code a little futher
What if you have:
highlight "indexed" terms.{ XE "higlight "indexed" terms" \b \i }
The code would only highlight the: " terms
I added some code to handled "one" pair of quotes in the indexed term:
Sub HighightBeforeXE()
Dim ofld As Field
Dim oRg As Range
Dim oTempRg As Range
Dim oCount As Integer
Dim ePos As Integer
Dim sPos As Integer
Dim midPos1 As Integer
Dim bQuotedIndex As Boolean
Dim midPos2 As Integer
Dim sWords As String
Dim sWordsArray As Variant
For Each ofld In ActiveDocument.Fields
bQuotedIndex = False
If ofld.Type = wdFieldIndexEntry Then
Set oRg = ofld.Code
sPos = InStr(5, oRg, ":", 1) + 1 'start position with subentry
If sPos < 6 Then 'no subentry
sPos = InStr(5, oRg, """") + 1 'start position
End If
midPos1 = InStr(sPos, oRg, "\""")
If midPos1 > 0 Then
bQuotedIndex = True
midPos2 = InStr(midPos1 + 2, oRg, "\""")
End If
If midPos2 > 0 Then
ePos = InStr(midPos2 + 2, oRg, """", 1)
Else
ePos = InStr(sPos + 1, oRg, """", 1) 'end pos
End If
sWords = Mid(oRg, sPos, ePos - sPos) 'words between the start" end"
marks.
sWordsArray = Split(sWords, " ")
If Not bQuotedIndex Then
oCount = UBound(sWordsArray) + 1
Else
oCount = UBound(sWordsArray) + 3
End If
With oRg
.Move unit:=wdWord, Count:=-1
.MoveStart unit:=wdWord, Count:=-oCount
.HighlightColorIndex = wdYellow
End With
Set oRg = Nothing
End If
Next ofld
End Sub
I had worked out something similiar using the Find.Execute method
before Jay introduced us to the the oFld.Code.Text range:
Public Sub HighlightIndexedTerms()
Dim oRg As Range
Dim oTempRg As Range
Dim i As Integer
Set oRg = ActiveDocument.Range
oRg.TextRetrievalMode.IncludeHiddenText = True
With oRg.Find
.ClearFormatting
.Text = " XE " ' find index entry field code
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchCase = False
Do While .Execute
'At this point, oRg covers the XE in an index field.
'Make another range that duplicates oRg...
Set oTempRg = oRg.Duplicate
'Use the duplicate to determine the indexed term.
With oTempRg
.Move unit:=wdWord, Count:=2 'Move range passed first "
Do
.MoveEndUntil Cset:=Chr(34), Count:=wdForward 'range the XE field
term
If oTempRg.Characters.Last = "\" Then
.MoveEnd unit:=wdCharacter, Count:=2
.MoveEndUntil Cset:=Chr(34), Count:=wdForward 'range the XE
field ter
End If
Loop Until oTempRg.Characters.Last <> "\"
i = oTempRg.Words.Count 'count words in indexed term
End With
Set oTempRg = oRg.Duplicate 'Again match oTempRg to oRg
With oTempRg
.Move unit:=wdWord, Count:=-1 'move range to left edge of indexed
term
'Expand the range to cover the indexed term
.MoveStart unit:=wdWord, Count:=-i
'Highlight it.
.HighlightColorIndex = wdYellow
End With
'The loop now continues from the current position of oRg, which
'hasn't been changed by fiddling with oTempRg.
Set oTempRg = Nothing
Loop
End With
End Sub
This would handle multiple quote pairs in the indexed term, but I
haven't attempted to address the subentries and references in the field
code.
HTH