?
Solved

Add VBA to clear text highlight during replace?

Posted on 2013-01-07
3
Medium Priority
?
734 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Suggested Courses

765 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