Find & Replace all words, except words in green

J

John Svendsen

Hi All:
I'm trying to build a Macro to replace all words in a document, except for
words whose color is green

I've tried the following code, first trying to use
"Selection.Find.Font.Color <> wdColorGreen" - but it doesn't like the "<>"
Then I though of replacing all colors except Green, but to my surprise ALL
words are changed (even the green words)

Could someone please shed some ligh on what I'm doing wrong? Or point me out
some other smarter way to go about this?
Thanks for your kind attention and help
Rgds, JS

Sub REPLACOLOR()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Font.Color = wdColorAutomatic
With Selection.Find
.Text = "UP"
.Replacement.Text = "DOWN"
.Forward = True
.Wrap = wdFindContinue
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
H

Helmut Weber

Hi John,

this one works for me:

Sub REPLACOLORX()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "UP"
.Font.Color = wdColorAutomatic
.Replacement.Text = "DOWN"
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Range(0, 0).Select
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jay Freedman

Try changing .Format to True.

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

John Svendsen

Hi Jay/Helmut

Works like a charm - I should've seen this, my bad :)

Do you have any idea on how to F&R all colors excepting a certain color
(instead of looping for all colors possible, minus the exception)?

Again, thanks!!!

JS
 
J

Jay Freedman

Hi John,

There are at least two ways to tackle that problem. One way is to loop
through all occurrences of "UP" and only replace the ones that are not
green:

Sub REPLACOLORX2()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "UP"
Do While .Execute
If rDcm.Font.ColorIndex <> wdGreen Then
rDcm.Text = "DOWN"
rDcm.Collapse wdCollapseEnd
End If
Loop
End With
End Sub

Another way is to use another format property that is only True or False,
such as Highlight, to mark the green occurrences; then change all the ones
that are not marked, and finally remove the marking:

Sub ReplaceColorY()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "UP"
.Font.ColorIndex = wdGreen
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
End With

Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "UP"
.Highlight = False
.Replacement.Text = "DOWN"
.Execute Replace:=wdReplaceAll
End With

Set rDcm = ActiveDocument.Range
With rDcm.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "UP"
.Highlight = True
.Replacement.Highlight = False
.Execute Replace:=wdReplaceAll
End With
End Sub

If your macro needs to run in Word 2007, you may have to deal with the fact
that what looks green may not be what Word understands as .Font.ColorIndex =
wdGreen, which is the specific color RGB(0,128,0). If you apply the "green"
standard color from the formatting palette, that's RGB(0,176,80) which won't
be matched by the .Find in the macro. Similarly, the "standard blue" isn't
RGB(0,0,255) but RGB(0,112,192). In Word 2003 and earlier there is no such
confusion.

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

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