Solved

Add VBA to clear text highlight during replace?

Posted on 2013-01-07
3
666 Views
Last Modified: 2013-01-07
Hello,

I am currently developing a template that will automatically replace text on a word document with text on a form.  The document has all items that need to be replaced or edited highlighted.  I am hoping to include in my vba some code that will unhighlight document text as it is replaced with word text.

Code snippet is as follows, so I would need to insert somewhere here I believe, what I have in there now is not getting rid of the highlight after the replace is done but is working to replace document text with form text:

Private Sub cmdCreateLetter_Click()
On Error Resume Next
Dim xCheckCounter As Long
Dim xCheckBoxName As Object
Dim ASOTemplate As Document
Set ASOTemplate = ActiveDocument
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "[STATE CONTRACTING ENTITY]"
        .Replacement.Text = txtState.Text
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
......<snip>
0
Comment
Question by:reportingdude
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 38751907
You can use the Highlight property in the Find and the Replacement parts. Note, avoid the using 'On Error Resume Next'. It is better to know about and to fix errors as  soon as possible.
Private Sub cmdCreateLetter_Click()
'On Error Resume Next
Dim xCheckCounter As Long
Dim xCheckBoxName As Object
Dim ASOTemplate As Document
Set ASOTemplate = ActiveDocument
    With ASOTemplate.Range.Find
        .Format = True
        .Text = "[STATE CONTRACTING ENTITY]"
        .Highlight = True
        With .Replacement
            .Text = txtState.Text
            .Highlight = False
        End With
        .Execute Replace:=wdReplaceAll
    End With
'......<snip>

Open in new window

0
 

Author Comment

by:reportingdude
ID: 38752195
Excellent, that worked beautifully.

Thanks!!!
0
 

Author Closing Comment

by:reportingdude
ID: 38752198
Perfect solution, exactly what I needed.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now