Re: search 1000 keywords in doc.
From: Jonathan West (jwest_at_mvps.org)
Date: 07/29/04
- Next message: Ed: "Re: Win/Word XP *always* asks for Enable - any help?"
- Previous message: Jonathan West: "Re: How to remove AutoOpen macro"
- In reply to: William: "search 1000 keywords in doc."
- Next in thread: William: "Re: search 1000 keywords in doc."
- Reply: William: "Re: search 1000 keywords in doc."
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 29 Jul 2004 23:32:03 +0100
Hi William,
Assuming that the majority of keywords are not present, you can do a
dramatic s[eedul;t like this
Sub ShowMyWords()
Dim lngCounter As Long
Dim strText As String
Dim vSearchTerms as Variant
'Open your keywords document
Documents.Open FileName:="c:\tmp\keywords.doc"
strText = ActiveDocument.Range.Text
If Right$(strText, 1) = vbCr Then
strText = Left$(strText, Len(strText) - 1)
End If
vSearchTerms = Split(strText, vbCr)
'Close your keywords document
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
strText = ActiveDocument.Range.Text
'Highlight the search terms in the active document.
For lngCounter = 0 to UBound(vSearchTerms)
If Instr(strText, vSearchterms) > 0 Then
With ActiveDocument.Content.Find
.Wrap = wdFindAsk
.Replacement.Highlight = True
.Execute FindText:=vSearchTerms(lngCounter), _
ReplaceWith:="", Replace:=wdReplaceAll, _
Forward:=True
End With
End If
Next lngCounter
End Sub
There are two main tricks here. I'll briefly describe them.
1. I'm retrieving the whole of the content of the keywords document in one
operation, and then manipulating the resulting string into separate items.
If you made the keywords document a text file, this could be done even more
quickly.
2. I'm also loading the whole of the text of the active document into a
string. I then check each search term against the string. This is *very
fast* compared to doing a Find against the document itself. Only if the
search against the string indicates a match does the code then do a Find on
the document,in order to highlighjt the terms which the code then knows will
be present. Unless more than 90% of the search terms are present in the
document, this will save time.
-- Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup "William" <William@discussions.microsoft.com> wrote in message news:9472D73A-65B2-4D07-8C65-83CDD7F789B2@microsoft.com... > >hi, > > > >good morning. i have a list of 1000 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 matches > 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? > > Dennis gave me the following code. is there anyway to speed up the search? > > >thanks for any idea. > > > >william > > > > > > -- > 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 > --------------- > > > >
- Next message: Ed: "Re: Win/Word XP *always* asks for Enable - any help?"
- Previous message: Jonathan West: "Re: How to remove AutoOpen macro"
- In reply to: William: "search 1000 keywords in doc."
- Next in thread: William: "Re: search 1000 keywords in doc."
- Reply: William: "Re: search 1000 keywords in doc."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|