find and change font colour?

G

Geoff Cox

Hello,

I would like to use a macro to find all green fonted words and change
them to blue ones.

How to do this? A few pointers would be appreciated!

Thanks

Geoff
 
G

Greg Maxey

Geoff,

If you are only interested in the main text part of your document then
something like this will do:

Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorGreen
.Replacement.Font.Color = wdColorBlue
.Execute Replace:=wdReplaceAll
End With
End Sub

If you are talking about "all" parts of the document e.g., headers,
foorters, textboxes, etc. then you will need a more complext macro that
process all the story ranges of the document. See:
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm

Also note that in Word2007 what may appear to be font wdColorGreen actually
has a specific theme color number (I guess that is correct). To find what
this number is, you will need to select a bit of the Green text and run this
code in the the VBE immediate window:

?Selection.Font.Color

The number returned can be used in place of wdColorGreen in the code above.
 
G

Geoff Cox

Geoff,

If you are only interested in the main text part of your document then
something like this will do:

Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorGreen
.Replacement.Font.Color = wdColorBlue
.Execute Replace:=wdReplaceAll
End With
End Sub

Thanks Greg!

The above works fine.

What I am trying to do is to find a series of section titles for which
the word or words has/have green fonts and to then give them a
particluar style called cgHeading.

The cgHeading style is listed under Word Styles and Formatting and is
part of the Course Genie program for changing Word docs into html
pages..

So far I have used record macro to simply change a green fonted word
to a cgHeadng style and get

Selection.Style = ActiveDocument.Styles("cgHeading")

How do I incorporate that into your code so that it will change one or
more green fonted words on a line into a cgHeading?

Cheers

Geoff
 
G

Graham Mayor

Greg has probably retired for the night by now - so use instead

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorGreen
.Replacement.Style = "cgHeading"
.Replacement.Font.Color = wdColorBlue
.Execute replace:=wdReplaceAll
End With
 
G

Geoff Cox

Greg has probably retired for the night by now - so use instead

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorGreen
.Replacement.Style = "cgHeading"
.Replacement.Font.Color = wdColorBlue
.Execute replace:=wdReplaceAll
End With

Wonderful! Thanks Graham.

Cheers

Geoff
 

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