Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

find next (green) highlighted word

396 views
Skip to first unread message

adgorn

unread,
Jul 19, 2006, 12:23:02 PM7/19/06
to
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!
--
Alan

Greg Maxey

unread,
Jul 19, 2006, 12:56:09 PM7/19/06
to
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

Greg Maxey

unread,
Jul 19, 2006, 1:01:52 PM7/19/06
to
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

adgorn

unread,
Jul 19, 2006, 4:29:02 PM7/19/06
to
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?

--
Alan

Greg Maxey

unread,
Jul 19, 2006, 5:54:29 PM7/19/06
to
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.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


adgorn wrote:
> 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?
>
>

Greg Maxey

unread,
Jul 20, 2006, 9:28:13 AM7/20/06
to
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

adgorn

unread,
Jul 20, 2006, 6:54:02 PM7/20/06
to
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.

Greg Maxey

unread,
Jul 20, 2006, 8:04:28 PM7/20/06
to
Send it.

Thanks.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


adgorn wrote:
> 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.
>

Greg Maxey

unread,
Jul 21, 2006, 8:51:23 AM7/21/06
to
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

sarr...@gmail.com

unread,
Apr 29, 2017, 4:22:56 PM4/29/17
to
I realize this is years old, but if anyone is still looking at this, I wondered if it is possible to modify it to find a particular color of highlight and replace it with a background color instead. (I'm converting from another program into Word, and that program makes everything into highlight. The highlights that it chooses are too dark.)
Thanks so much in advance.
Sarra
0 new messages