Solved

Add VBA to clear text highlight during replace?

Posted on 2013-01-07
3
661 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

895 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

16 Experts available now in Live!

Get 1:1 Help Now