Solved

MS Word Macro to Highlight Text

Posted on 2009-05-12
6
900 Views
Last Modified: 2012-06-27
Hi,

I need to write a macro or other code to achieve the following goal when a document is opened.

1) Display the word document
2) Highlight any words in the document which are in the clipboard.

The request sound simple enough but thus far I am stumped as how to approach this.

Here is a samle of the clipboard contents:
Index of INC_DOCS  -  telephone and perry

P:\PUBLIC\INC_DOCS\86804.doc      6 Hits
P:\PUBLIC\INC_DOCS\36905.doc      4 Hits
P:\PUBLIC\INC_DOCS\97053.doc      3 Hits
P:\PUBLIC\INC_DOCS\86083.doc      2 Hits
P:\PUBLIC\INC_DOCS\53412.doc      2 Hits
P:\PUBLIC\INC_DOCS\62894.doc      2 Hits
P:\PUBLIC\INC_DOCS\93763.doc      3 Hits
P:\PUBLIC\INC_DOCS\46199.doc      3 Hits
P:\PUBLIC\INC_DOCS\80905.doc      2 Hits
P:\PUBLIC\INC_DOCS\90660.doc      3 Hits
P:\PUBLIC\INC_DOCS\97363.doc      3 Hits
P:\PUBLIC\INC_DOCS\85723.doc      7 Hits
P:\PUBLIC\INC_DOCS\96224.doc      3 Hits
P:\PUBLIC\INC_DOCS\85723.rtf      4 Hits
P:\PUBLIC\INC_DOCS\86177.doc      2 Hits
P:\PUBLIC\INC_DOCS\76956.doc      2 Hits
P:\PUBLIC\INC_DOCS\83505.doc      2 Hits
P:\PUBLIC\INC_DOCS\91948.doc      3 Hits
P:\PUBLIC\INC_DOCS\94205.doc      2 Hits
P:\PUBLIC\INC_DOCS\96517.doc      2 Hits
0
Comment
Question by:systemssupply
  • 3
  • 2
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24368903
The approach that I would take is to open a new document, and paste the clipboard contents therein.

Then I would take each line and do a Find and Replace in the original document, the Replace to apply a highlighting colour to anything found.
0
 

Author Comment

by:systemssupply
ID: 24369094
That sound logical but raises another question. In my example "Index of INC_DOCS  -  telephone and perry "

If I run a find and replace  the whole string will not be in the document. Rather, the words telephone, perry will be in the document.

How can I tell the find / replace to search for any of the words not the whole string?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24369385
I'm not quite sure that I understand the problem, but you if you want to find one or another of two words, you can do two successive searches.
0
 

Author Comment

by:systemssupply
ID: 24370906
True, I could run two searches however, in my example I had two words in a boolean search setup. It could also be additional words. I think that I need to find a way to export these terms into some sort of array to search on.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 24372232
If the order of the words is important you could try using wildcards to check and then
do a highlight find and replace on each word:

Find: telephone*perry

Otherwise you would have to check each word and only highlight the words if all were found.
Sub CallMultiFind()

    Dim SomeWords(1) As Variant

    

    SomeWords(0) = "telephone"

    SomeWords(1) = "perry"

    MultiFind ActiveDocument, SomeWords

End Sub
 

Sub MultiFind(Doc As Document, TheWords())

    Dim i As Integer

    

    For i = 0 To UBound(TheWords)

        If Not Doc.Range.Find.Execute(TheWords(i)) Then

            Exit Sub

        End If

    Next i

    For i = 0 To UBound(TheWords)

        With Doc.Range.Find

        .Text = TheWords(i)

        .Replacement.Highlight = True

        .Execute Replace:=wdReplaceAll

        End With

    Next i

End Sub

Open in new window

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

Suggested Solutions

Title # Comments Views Activity
Text wrapping issue with Word 14 40
MailMerge VBA DataSource Connection Issue 10 50
Word 2010 mail merge 3 65
How to search text in Word documents in Windows 10 3 47
When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
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…
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) …
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.

863 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

23 Experts available now in Live!

Get 1:1 Help Now