VBA In Word - Replace Text With Formatting

LM100

New Member
Joined
Aug 3, 2008
Messages
13
Hi,

I'm trying to find a number of phrases in a document and colour them according to which phrase.

Using macro recorder, I've made the following but I'm wondering if there's a clear way of writing the code so more phrases can be added at a later date.
Sub ColourTextMulti()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = wdColorBlue
With Selection.Find
.Text = "Phrase One"
.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
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = wdColorRed
With Selection.Find
.Text = "Phrase Two"
.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

Could this be written in a form that listed

"Phrase One" = wdColorRed
"Phrase Two" = wdColorBlue

....etc?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi LM,

It's not exactly clear whether you merely want to simplify the addition of more expressions, each with different colourings, or you want to be able to apply a range of colourings to multiple find strings. For the first, you could do something along the lines of the following:
Code:
Sub ColourTextMulti()
With Selection.Find
  .ClearFormatting
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Replacement.ClearFormatting
  .Replacement.Text = "^&"
  .Text = "Phrase One"
  .Replacement.Font.Color = wdColorBlue
  .Execute Replace:=wdReplaceAll
  .Text = "Phrase Two"
  .Replacement.Font.Color = wdColorRed
  .Execute Replace:=wdReplaceAll
  .Text = "Phrase Three"
  .Replacement.Font.Color = wdColorGreen
  .Execute Replace:=wdReplaceAll
End With
End Sub
For some ideas on how to go about the second, see the code I posted at: http://www.vbaexpress.com/forum/showthread.php?t=33793&page=2
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,144
Members
452,891
Latest member
JUSTOUTOFMYREACH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top