searching keywords in a doc.

D

DA

Hi William

There are a few different ways you could do this, but
here's one example on how you can do your highlighting.
I've used a document called "Keywords.doc", which
contains single word paragraphs for each of the search
terms. Note that you'll have to change file and path info
to suit your setup.

Paste this code into the document where you want to do
your highlighting. Look at the code comments, but it's
fairly basic, so I hope you can follow it without any
problems.

The routine fills an array from the keywords doc and then
uses that data in a simple find/replace.

Hope that helps,
Dennis

---------------
Sub ShowMyWords()
Dim lngCounter As Long
Dim strText As String
Dim astrSearchTerms() As String

'Open your keywords document
Documents.Open FileName:="c:\tmp\keywords.doc"

'Size an array to the number of terms
lngParaCount = ActiveDocument.Paragraphs.Count
ReDim astrSearchTerms(lngParaCount - 1)

'Fill the array with the search terms
For lngCounter = 1 To lngParaCount
strText = ActiveDocument.Paragraphs(lngCounter). _
Range.Text
astrSearchTerms(lngCounter - 1) = _
Left(strText, Len(strText) - 1)
Next

'Close your keywords document
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

'Highlight the search terms in the active document.
lngCounter = 0
While astrSearchTerms(lngCounter) <> ""
With ActiveDocument.Content.Find
.Wrap = wdFindAsk
.Replacement.Highlight = True
.Execute FindText:=astrSearchTerms(lngCounter), _
ReplaceWith:="", Replace:=wdReplaceAll, _
Forward:=True
End With
lngCounter = lngCounter + 1
Wend
End Sub
---------------


-----Original Message-----
hi,

good morning. i have a list of keywords to search for in
a word doc. if there are matches in the doc., i'd like to
mark them (make selection?), and to highlight the matched
words wt colors.
in vba for word xp, could i get a list of words from a
doc., know the location of the words, and be able to
highlight them if necessary?
 
H

Helmut Weber

Hi William,
interesting...
could you send me two sample docs?
"h.weber" & chr(64) & "mi-verlag.de"
Though I think, Dennis' approach is a pretty good one.
Don't know, whether there is a faster solution,
but would like to find it out.
How much time does it take with your equipment?
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top