Google Answers Logo
View Question
 
Q: MS Word: How to get a macro to change text in custom color? ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: MS Word: How to get a macro to change text in custom color?
Category: Computers > Software
Asked by: rambler-ga
List Price: $8.00
Posted: 23 Aug 2006 09:59 PDT
Expires: 22 Sep 2006 09:59 PDT
Question ID: 758761
Everyday, I modify a Word document that contains a lot of text in
fonts and colors that I don't like. It's a nuisance making the same
changes day after day, but when I try to record the changes in a
macro, it doesn't work: the macro simply fails to record actions
dealing with text colors.

I do know, however, how to modify the macro and force it to work.  For
example, the following code, will change all pre-selected text that is
non-italic and automatic color into bold green:
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Font.Color = wdColorAutomatic
        .Font.Italic = False
        .Wrap = wdFindStop
    End With
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find.Replacement
        .Text = ""
        .Font.Color = wdColorGreen
        .Font.Bold = wdToggle
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

So, what's my problem? Well, the text is in a "custom color", and I
don't know how to specify custom colors in a macro.

When I do these changes 'manually', the Find And Replace dialogue box
says this (just before I hit the "Replace All" button):
  Find what:    Format: Font: Bold, Font color: Custom Color(RGB(101,101,101))
  Replace with: Format: Font: Not Bold, Not Italic, Font color: Auto

It's that "RGB" stuff that I don't know how to specify in a macro.  Can you help?
Answer  
Subject: Re: MS Word: How to get a macro to change text in custom color?
Answered By: maniac-ga on 23 Aug 2006 16:15 PDT
Rated:5 out of 5 stars
 
Hello Rambler,

The specific method for an RGB color value in Visual Basic is to use
the "RGB" function. As described in part of the Visual Basic online
help (search phrase RGB, select RGB function)
  
  RGB(red, green, blue)
  The RGB function syntax has these named arguments:
  Part  Description
  red  Required; Variant (Integer). Number in the range 0-255,
inclusive, that represents the red component of the color.
  green  Required; Variant (Integer). Number in the range 0-255,
inclusive, that represents the green component of the color.
  blue  Required; Variant (Integer). Number in the range 0-255,
inclusive, that represents the blue component of the color.

For your example, the rewrite would be something like
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Font.Color = RGB(101, 101, 101)
        .Font.Italic = False
        .Wrap = wdFindStop
    End With

I am somewhat surprised that your version of MS Word did not properly
record the replacement. Which version are you using?

On my system I did something similar and had recorded

Sub A()
'
' A Macro
' Macro recorded 8/23/06 by Maniac
'
    Selection.Find.ClearFormatting
    Selection.Find.Font.Color = 6645093
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find.Replacement.Font
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .Color = wdColorAutomatic
    End With
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

The value of RGB(101, 101, 101) can be confirmed by the following
immediate window command

print rgb(101,101,101)
 6645093 

which matches the value used above.

Please make a clarification request if any part of the answer is
unclear or if you need further information about this topic. I would
be glad to help further.

Good luck with your work.
  --Maniac
rambler-ga rated this answer:5 out of 5 stars and gave an additional tip of: $2.00
Exactly what I needed. Thank you!

Comments  
Subject: Re: MS Word: How to get a macro to change text in custom color?
From: jefffromgreen-ga on 23 Aug 2006 14:01 PDT
 
In this line
    .Font.Color = wdColorGreen

Have you tried using 
    .Font.Color = &H0000FF00& (Green)

    .Font.Color = &H000000FF& (Red)

    .Font.Color = &H00FF0000& (Blue)
or 
    .Font.Color = &H000CC0FF& (kind of a mustard)

You can "Mix" the colors by changing the Blue, Red, and Green values
from 00 to FF hex.

JeffFromGreen

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy