Select Word Range Depending on Color

M

morau99

I have a document that has blocks of black text alternating with blocks
of red text (black -> red -> black -> red, etc.). I need to
programatically select the first block of contiguous black text (and
then use the selected text for something), then the next block of
contiguous red text, then the next block of contiguous black text, etc.
until there is no more text in the document. As a novice in word, I
have no idea how to extend a selection in this manner. Please help!
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
I have a document that has blocks of black text alternating with
blocks of red text (black -> red -> black -> red, etc.). I need to
programatically select the first block of contiguous black text (and
then use the selected text for something), then the next block of
contiguous red text, then the next block of contiguous black text,
etc. until there is no more text in the document. As a novice in
word, I have no idea how to extend a selection in this manner.
Please help!

There is probably a faster way, but right now this is all I can think of...

'_______________________________________
Application.ScreenUpdating = False

Dim BlockRange As Range
Dim CheckNextCar As Range
Dim CurColor As Long

Set BlockRange = ActiveDocument.Range(0, 1)
CurColor = BlockRange.Font.Color

With BlockRange
Do
Set CheckNextCar = .Next(wdCharacter, 1)
If CheckNextCar.Font.Color = CurColor _
And .End < ActiveDocument.Range.End - 1 Then
.MoveEnd wdCharacter, 1
Else
.Font.Bold = True
CurColor = CheckNextCar.Font.Color
.Collapse wdCollapseEnd
If .End = ActiveDocument.Range.End - 1 Then Exit Do
End If
Loop
End With

Application.ScreenRefresh
Application.ScreenUpdating = False
'_______________________________________

Replace
.Font.Bold = True
with code that will execute what you actually want to do with the block of
text.
Keep in mind that as we have to scan each character one by one, this is
slow.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi,
I have a document that has blocks of black text alternating with blocks
of red text (black -> red -> black -> red, etc.).

are you sure, that even spaces are red or black,
and that the color of the black text isn't automatic?
(font.color = wdcolorautomatic)
--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
K

Klaus Linke

Hi moreau,

There's also a built-in method for this:
Selection.SelectCurrentColor

Once you have processed that, you'd collapse the selection
Selection.Collapse(wdCollapseEnd)
and continue with the next block.

Regards,
Klaus
 
J

Jean-Guy Marcil

Klaus Linke was telling us:
Klaus Linke nous racontait que :
Hi moreau,

There's also a built-in method for this:
Selection.SelectCurrentColor

Once you have processed that, you'd collapse the selection
Selection.Collapse(wdCollapseEnd)
and continue with the next block.

Ha! I knew there had to be an easier way!
But you have to use the Selection object... I guess you have to sometimes!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

Klaus Linke

Hi Jean-Guy,
But you have to use the Selection object... I guess you have to sometimes!

Not with your macro! <g>

..SelectCurrentColor a method that probably only was added to VBA since it
duplicates a very old WordBasic command.

If you use a macro, you could also check the color at the IP, and use
Range.Find to select.
I'm not sure though whether Find would select all the following text in that
color, or just to the end of the paragraph.

Regards,
Klaus
 

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