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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,215,771
Messages
6,126,798
Members
449,337
Latest member
BBV123

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