Re: search 1000 keywords in doc.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jonathan West (jwest_at_mvps.org)
Date: 07/29/04


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
> --------------- 
>
>
>
>


Relevant Pages

  • search 1000 keywords in doc.
    ... Dim lngCounter As Long ... 'Open your keywords document ... 'Close your keywords document ... 'Highlight the search terms in the active document. ...
    (microsoft.public.word.vba.general)
  • Re: Algoritim - Create Unique Integer from String
    ... back to the original string), only that a unique string produce a unique ... spam categorizer uses some sort of one-way hash for its Naive Bayes ... It breaks the messages up into keywords, ... before it stores them in the database (or compares them to existing ...
    (comp.programming)
  • RE: Extracting keywords from a string
    ... Keywords table. ... Function GetLongWords(ByVal strText As String, ... Dim intSpacePos As Integer ... Dim strWord As String, strWordList As String ...
    (microsoft.public.access.queries)
  • Re: Negate a character sequence in a regular expression?
    ... The engine starts scanning through the string until matches 'cat'. ... where cat and dog were separated by only a few ... but the frequency of appearance of the keywords ... you need to add the /m modifier if your strings are paragraphs. ...
    (comp.lang.ruby)
  • Re: Contains clause with only NOT keywords
    ... For your date query it would look like this ... You cannot search on a date string and hope for it to be interpreted as ... I am designing a search screen that searches for keywords in Text ... I also tried separating out the NOT keywords into a series of " and not ...
    (microsoft.public.sqlserver.fulltext)