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
Solved

Add VBA to clear text highlight during replace?

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word - Access 6 67
recent docs in word and excel 3 53
Resetting Word when a document is closed 5 47
microsoft word short cuts 3 47
Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

789 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