change font color macro

K

Karen Sigel

In Word 2003, I've written a macro to change specific parts of the text in a
document to Dark Red (RGB 128,0,0) with a Find/Replace. I tested and created
the Find/Replace first, then duplicated the actions to record the macro.
When I do the F/R manually, it works just fine; it also works while I'm
recording it. After I finish the recording and run the macro, however, it
does everything EXCEPT change the color. What am I doing wrong and how can I
fix it?

Here's the text of my macro:

' FixResumeProjectName Macro
' Macro recorded 6/25/2008 by klsigel
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "---*---"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "---"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Thanks-
Karen
 
J

Jay Freedman

A bug in the recorder causes it to omit the formatting settings (any
formatting, bold or italic or color, etc.), so your recorded macro doesn't
contain any reference to the color you asked for.

Look at the "Fixing broken Replace macros" section of
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.

The statement you need to add (to the With Selection.Find group of each of
the two replacements) is

.Font.Color = wdColorDarkRed

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
K

Karen Sigel

OK, I'm totally confused now. I've copied your statement to where I thought
it should go based on your reply and your article, and it still doesn't work.
This is what I have at the moment:

Sub DarkRedResumeText()
'
' DarkRedResumeText Macro
' Macro recorded 6/25/2008 by klsigel
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "---*---"
.Replacement.Text = "^&"
.Font.Color = wdColorDarkRed
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "---"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Thanks for your help-
Karen
 
K

Karen Sigel

Jonathan-

Thank you. I read the article, but my issue isn't what to do with a whole
macro sent to me by someone else; the macro is mine. It's supposed to find
all the text between --- and ---, replace it with itself, and change it to
font color dark red. It does everything except change the font color.

What I need to know is, where do I place the statement sent by Jay
(.Font.Color = wdColorDarkRed) into my macro. I've placed it where I think
it should go, but it's not doing what I want, which is to change the Find
What Text (^&) to Dark Red.

Karen
 
K

Karen Sigel

Jonathan-

Sorry, I my description of the macro's intent wasn't complete; after it does
what i mentioned, it finds and replaces --- with "" (nothing).

K
 
J

Jay Freedman

I'm the one that should be sorry, for not checking my work closely enough.

You need three different fixes in this macro:

- The line
.Font.Color = wdColorDarkRed
should be this instead:
.Replacement.Font.Color = wdColorDarkRed

- You also need the line
.Replacement.Font.Color = wdColorDarkRed
in the second search, the one that looks for "---".

- In the second search, the line
.Replacement.Text = ""
should be this instead:
.Replacement.Text = "^&"
the same as it appears in the first search.

Explanations: The expression .Font.Color sets the color that the search
looks for (as if you had specified a color in the Find What box). What you
want instead is the expression .Replacement.Font.Color to set the color of
the replacement. Since you have two searches, that expression needs to be
set in each of them.

The line .Replacement.Text = "" means "replace the found text with nothing"
which is the same as deleting the found text. Instead, you need the line
..Replacement.Text = "^&" which says "replace the found text with the same
text" and just change the formatting.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
K

Karen Sigel

Jay-

That makes so much more sense — and it works.

One question: Why, in the second search, do I have to use ^& instead of ""?
All I'm doing in that part of the F/R is deleting all the dashes, which are
only there to mark the bits of text I want to change to dark red. So,
replace "---" with "nothing". Yes?

And, it works just fine when I don't....

Thanks!
Karen
 
J

Jay Freedman

Oh, I just misread your post about 'it finds and replaces --- with ""
(nothing)', thinking that this was an unwanted result. Certainly, if you want to
remove the dashes, then replacing them with nothing is the correct step.
 

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