Sherching for a color and inserting text.

M

metim

I have a rather large word document that has several words colored in Red and
Green.
I would like to insert a word a the beginning of every paragraph that
contains a red or a green word.
For example the word "[RED]" at the beginning of every paragraph containing
a word colored in red text and the word "[Green]" in front of every paragraph
that containing a word colored in green text.

I hope the above makes sense, any help is much appreciated

Thanks in advance
 
K

Klaus Linke

Hi metim,

Let me assume all the text is left-aligned. If it isn't, you'd need to use
another paragraph formatting than alignment in the next steps, but you could
still use the same general strategy.

You could then do it with two replacements:

1. -- Replace "red" with "right-aligned".

Paragraphs that contain red text (and only these) are now right-aligned.

2. -- With "Match wildcards" checked,
search for [!^13]@^13 (plus Format > Paragraph > Alignment=Right) and
replace with [RED]^& (plus Format > Paragraph > Alignment=Left).

Regards,
Klaus
 
M

metim

Klaus,
Thanks for the quick responce. I had hoped to achive this ussing a macro, as
there may be more then just 2 colors.
I had a quick attempt for the red option only my code is below, but this
getts stuck in a infernate loop. any surgestions


Sub Macro3()
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Text = ""
.Font.Color = wdColorRed
.Forward = True
.Wrap = wdFindStop
Do While .Execute
With Selection.Paragraphs
Selection.Collapse wdCollapseStart

MyText = "[RED] "
' Selection Example:
Selection.InsertBefore (MyText)
End With
Loop

End With

Klaus Linke said:
Hi metim,

Let me assume all the text is left-aligned. If it isn't, you'd need to use
another paragraph formatting than alignment in the next steps, but you could
still use the same general strategy.

You could then do it with two replacements:

1. -- Replace "red" with "right-aligned".

Paragraphs that contain red text (and only these) are now right-aligned.

2. -- With "Match wildcards" checked,
search for [!^13]@^13 (plus Format > Paragraph > Alignment=Right) and
replace with [RED]^& (plus Format > Paragraph > Alignment=Left).

Regards,
Klaus


metim said:
I have a rather large word document that has several words colored in Red
and
Green.
I would like to insert a word a the beginning of every paragraph that
contains a red or a green word.
For example the word "[RED]" at the beginning of every paragraph
containing
a word colored in red text and the word "[Green]" in front of every
paragraph
that containing a word colored in green text.

I hope the above makes sense, any help is much appreciated

Thanks in advance
 
K

Klaus Linke

Your code will be a bit slow on long documents, but if your documents aren't
that long and speed isn't an issue, it'll be great.
Else, you could do my two replacements in a macro just as easily...

I've fixed the issues in the code below:

Dim myText As String
myText = "[RED] "
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Text = ""
.Font.Color = wdColorRed
.Forward = True
.Wrap = wdFindStop
Do While .Execute
Selection.Paragraphs(1).Range.InsertBefore myText
Selection.Collapse wdCollapseEnd
Loop
End With

The main issue was:
Selection.Find will search from the current selection downwards, so you need
to collapse the selection to the end after each match, else the same match
is found again and again.

Regards,
Klaus



metim said:
Klaus,
Thanks for the quick responce. I had hoped to achive this ussing a macro,
as
there may be more then just 2 colors.
I had a quick attempt for the red option only my code is below, but this
getts stuck in a infernate loop. any surgestions


Sub Macro3()
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Text = ""
.Font.Color = wdColorRed
.Forward = True
.Wrap = wdFindStop
Do While .Execute
With Selection.Paragraphs
Selection.Collapse wdCollapseStart

MyText = "[RED] "
' Selection Example:
Selection.InsertBefore (MyText)
End With
Loop

End With

Klaus Linke said:
Hi metim,

Let me assume all the text is left-aligned. If it isn't, you'd need to
use
another paragraph formatting than alignment in the next steps, but you
could
still use the same general strategy.

You could then do it with two replacements:

1. -- Replace "red" with "right-aligned".

Paragraphs that contain red text (and only these) are now right-aligned.

2. -- With "Match wildcards" checked,
search for [!^13]@^13 (plus Format > Paragraph > Alignment=Right) and
replace with [RED]^& (plus Format > Paragraph > Alignment=Left).

Regards,
Klaus


metim said:
I have a rather large word document that has several words colored in
Red
and
Green.
I would like to insert a word a the beginning of every paragraph that
contains a red or a green word.
For example the word "[RED]" at the beginning of every paragraph
containing
a word colored in red text and the word "[Green]" in front of every
paragraph
that containing a word colored in green text.

I hope the above makes sense, any help is much appreciated

Thanks in advance
 

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