Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WORD: Highlight replaced text using VBA code 1

Status
Not open for further replies.

Excelerate2004

Programmer
Mar 8, 2004
163
CA
Hello,

I'm trying to use VBA code via a button click sub procedure in MS word 2000 to highlight text that has just been replaced via a find & replace.

I used a Macro just to see the syntax for highlighted text and got the following:

<code>
Selection.Range.HighlightColorIndex = wdYellow
</code>

However, I want to use it in this context:

<code>
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "M"
.Replacement.Text = "[A/C]"
<i>.HighlightColorIndex = wdYellow</i>
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
</code>

Is this highlighting of recently replaced text possible??
 
Hi Excelerate2004,

You need to do a couple of things.

1. You cannot specify a highlight colour on the Replace - it uses the default, so you must change the default first.
2. To highlight replaced text, set .Format = True and .Replacement.Highlight = True.

Complete code then becomes
Code:
[blue]    [highlight]Options.DefaultHighlightColorIndex = wdYellow[/highlight]
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "M"
        .Replacement.Text = "[A/C]"
        [highlight].Replacement.Highlight = True[/highlight]
        .Forward = True
        .Wrap = wdFindAsk
        .Format = [highlight]True[/highlight]
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Tony is correct, you need to set the highlightcolor first - just in case it is NOT the color you want. You can integrate it even more with:
Code:
Sub ReplaceThenHighlight()
Selection.HomeKey unit:=wdStory
Options.DefaultHighlightColorIndex = wdYellow
With ActiveDocument.Range.Find
 .ClearFormatting
 With .Replacement
   .Highlight = True
 End With
 Do While (.Execute(findtext:="M", _
   Forward:=True, _
   ReplaceWith:="[A/C]", _
   Replace:=wdReplaceAll) = True) = True
 Loop
End With
End Sub

Gerry
 
Hi Gerry,

Your use of =True = True fascinates me.

How does
Code:
Do While (.Execute(Replace:=wdReplaceAll) = True) = True
differ from
Code:
Do While .Execute(Replace:=wdReplaceAll)

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Thanks guys,

I would never have thought to put in this line:

Options.DefaultHighlightColorIndex = wdYellow

A simple solution!

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top