find next (green) highlighted word

A

adgorn

I have a technical document in which I have highlighted numerous words in
bright green. These words may need to be changed for each client engagement
that we do. So I need a way to "find next/select" each green highlighted
word one at a time so that we can edit them.

Also, some of these green highlighted words begin with a special character
(caret ^) denoting that they are already updated. If possible, it would be
really great if I could exclude those words from the "find next green
highlighted" process.

Thanks in advance!
 
G

Greg Maxey

This is rather crude and looks for highlight (of any color). If you
set your cursor at the start of the document to begin and after each
edit ensure it is after the last edited word then it might work for
you. You will need to trigger it initially and after each edit with a
keyboard command, menu or toolbar shortcut:

Sub Test()
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Set oRng1 = ActiveDocument.Range
Set oRng2 = Selection.Range
oRng1.Start = oRng2.End
Continue:
With oRng1.Find
.Highlight = True
.Execute
oRng1.Select
If oRng1.Characters.First = "^" Then
oRng1.Collapse wdCollapseEnd
GoTo Continue
Else
Selection.Collapse wdCollapseStart
End If
End With
End Sub
 
G

Greg Maxey

If you had mixed highlighting and had wanted to stop at only
brightgreen, you might use:
Sub Test()
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Set oRng1 = ActiveDocument.Range
Set oRng2 = Selection.Range
oRng1.Start = oRng2.End
Continue:
With oRng1.Find
.MatchWildcards = True
.Highlight = True
.Execute
oRng1.Select
If oRng1.Characters.First = "^" Or _
oRng1.HighlightColorIndex <> wdBrightGreen Then
oRng1.Collapse wdCollapseEnd
GoTo Continue
Else
Selection.Collapse wdCollapseStart
End If
End With
End Sub
 
A

adgorn

WOW, it works - carets, mixed highlighting and all! Thanks.

A couple of minor nits:
1) The document is a mixture of text in paragraphs and in tables. It
doesn't want to jump to the next line of a table but keeps scanning the same
row of the table.
2) When it gets to the end of the document, it seems to hang up and I have
to cntrl-brk. Any code to insert to recognize the end and shut off the macro?

Enhancement request:
Instead of the I-beam ending up in front of the word, can we select the word
instead?
 
G

Greg Maxey

I haven't figured out the table bit and feeling as if I can't :-(.

As for the rest:

Sub Test()
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Set oRng1 = ActiveDocument.Range
Set oRng2 = Selection.Range
oRng1.start = oRng2.End
Continue:
With oRng1.Find
.MatchWildcards = True
.Highlight = True
.Execute
oRng1.Select
If oRng1.Characters.First = "^" Or _
oRng1.HighlightColorIndex <> wdBrightGreen Then
oRng1.Collapse wdCollapseEnd
If oRng1.End + 1 = ActiveDocument.End Then Exit Sub
GoTo Continue
' Else
' Selection.Collapse wdCollapseStart
End If
End With
End Sub

Note: If you have the Tools>Options>Edit>Typing Replaces Selection set then
if you start typing with the word selected, it will lose the highlighting.
 
G

Greg Maxey

Try:

Sub StepThroughRange()
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Dim oRowIndex As Long
Dim oColIndex As Long
Set oRng1 = ActiveDocument.Range
Set oRng2 = Selection.Range
oRng1.start = oRng2.End
If Not oRng2.Information(wdWithInTable) Then
Continue:
With oRng1.Find
.Highlight = True
.Execute
oRng1.Select
If oRng1.Characters.First = "^" Or _
oRng1.HighlightColorIndex <> wdBrightGreen Then
oRng1.Collapse wdCollapseEnd
If oRng1.End + 1 = ActiveDocument.Range.End Then
Selection.Collapse wdCollapseEnd
Exit Sub
End If
GoTo Continue
Else
Selection.Collapse wdCollapseStart
End If
End With
Else
Continue2:
oRowIndex = Selection.Information(wdStartOfRangeRowNumber)
oColIndex = Selection.Information(wdStartOfRangeColumnNumber)
Set oRng1 = Selection.Tables(1).Cell(oRowIndex, oColIndex).Range
Set oRng2 = Selection.Range
oRng1.MoveEnd wdCharacter, -1
oRng1.start = oRng2.End
With oRng1.Find
.Highlight = True
.Execute
oRng1.Select
If oRng1.Characters.First = "^" Or _
oRng1.HighlightColorIndex <> wdBrightGreen Then
oRng1.Collapse wdCollapseEnd
If oRng1.End + 1 = Selection.Tables(1).Cell(oRowIndex,
oColIndex).Range.End Then
Selection.Collapse wdCollapseEnd
Selection.Move wdCharacter, 1
End If
If Selection.Range.End + 1 <> Selection.Tables(1).Range.End Then
GoTo Continue2
Else
GoTo Continue
End If
Else
Selection.Collapse wdCollapseStart
End If
End With
End If
End Sub
 
A

adgorn

Works pretty well and will be useful, but still fails to find all instances
w/i the tables. I'm sure you have other things you would prefer to do, but
I'd be happy to send you a document to test/debug this with. Thank you very
much for your efforts.
 
G

Greg Maxey

Yes that last one was a bit buggy. Try:

Sub StepThroughRange()
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Dim oRowIndex As Long
Dim oColIndex As Long
Set oRng1 = ActiveDocument.Range
Set oRng2 = Selection.Range
oRng1.start = oRng2.End
Do
NextRow:
If oRng2.Information(wdWithInTable) Then
Set oRng2 = Selection.Range
oRowIndex = Selection.Information(wdStartOfRangeRowNumber)
oColIndex = Selection.Information(wdStartOfRangeColumnNumber)
On Error GoTo Handler
Set oRng1 = Selection.Tables(1).Cell(oRowIndex, oColIndex).Range
oRng1.MoveEnd wdCharacter, -1
oRng1.start = oRng2.End
With oRng1.Find
.Highlight = True
.Execute
oRng1.Select
If oRng1.Characters.First = "^" Or _
oRng1.HighlightColorIndex <> wdBrightGreen Then
oRng1.Collapse wdCollapseEnd
If oRng1.End + 1 = Selection.Tables(1).Cell _
(oRowIndex, oColIndex).Range.End Then
Selection.Collapse wdCollapseEnd
Selection.Move wdCharacter, 1
End If
If Selection.Range.End + 1 = Selection.Tables(1).Range.End Then

Selection.Move wdCharacter, 1
Set oRng2 = Selection.Range
Exit Do
End If
Else
Selection.Collapse wdCollapseStart
Exit Do
End If
End With
Else
Exit Do
End If
Loop
If Not oRng2.Information(wdWithInTable) Then
With oRng1.Find
.Highlight = True
Do
.Execute
oRng1.Select
If oRng1.Information(wdWithInTable) Then Exit Sub
If oRng1.Characters.First = "^" Or _
oRng1.HighlightColorIndex <> wdBrightGreen Then
oRng1.Collapse wdCollapseEnd
If oRng1.End + 1 = ActiveDocument.Range.End Then
Selection.Collapse wdCollapseEnd
Exit Sub
End If
Else
Selection.Collapse wdCollapseStart
Exit Do
End If
Loop
End With
End If
Exit Sub
Handler:
If Err.Number = 5941 Then
Selection.Move wdCharacter, 1
Resume NextRow
End If
End Sub





Works pretty well and will be useful, but still fails to find all instances
w/i the tables. I'm sure you have other things you would prefer to do, but
I'd be happy to send you a document to test/debug this with. Thank you very
much for your efforts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top