Results 1 to 5 of 5

Thread: Search for multiple words

  1. #1
    VBAX Newbie
    Joined
    Mar 2005
    Posts
    5
    Location

    Search for multiple words

    I've got a list of words that sit in an array. I need to search through a word document one paragraph at a time with all the word that are in the array. The problem that Im having, is that it finds the first one, but does not continue with the rest of the words in the array.

    Does anybody know of an example on the web that can help me out with this one?

    Regards

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    You should post the code you have so far... it maybe something simple like using .FindNext having executed the Find object but a lot depends on how you've done it
    K :-)

  3. #3
    VBAX Newbie
    Joined
    Mar 2005
    Posts
    5
    Location
    Im actually writing this in C# using the com/interop objects. Ive posted the code below, maybe you can figure out where Im going wrong. Im not a VBA expert, but it almost look as if the "Find object" has to be reset on each interval of the loop.


    Word.ApplicationClass publication = new Word.ApplicationClass();
    object fileName = @"C:/Temp/test.doc";
    publication.Visible = false;
    object missing = Type.Missing;
    object myFalse = false;
    object myTrue = true;
    object saveChanges = false;

    Word.Document analyseforData = publication.Documents.Open(ref fileName, ref missing, ref myFalse, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref myFalse,
    ref missing, ref missing, ref missing, ref missing);

    for (int i = 1; i < analyseforData.Paragraphs.Count; i++) {

    //select the position within the document
    object startpos = i;
    object endpos = i;
    Word.Range range = analyseforData.Range(ref startpos, ref endpos);
    range.Select();
    Word.Find find = range.Find;

    // Test array
    string[] myArray = new string[3];
    myArray[0] = "Red";
    myArray[1] = "Blue";
    myArray[2] = "Green";

    // loop through the array testing on the data
    for(int j =0; j < myArray.Length;j++){
    object finddata = myArray[j];
    find.Execute(ref finddata, ref myFalse, ref myFalse, ref myFalse, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing);

    //check to see what we have found
    if (find.Found) {
    tb.AppendText("FOUND:" + find.Text + "\r\n");
    } else {
    tb.AppendText("Hint:did not find " + find.Text + "\r\n");
    }
    }
    }

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I don't know C# but I suspect your problem is because execution of the Find object redefines the Range. Try setting it to the whole paragraph at the beginning of the "for(int j=0; ..." loop (i.e. at the start of processing each search string) instead of just once before the loop.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Sorry Jake, I was getting my Find objects confused.
    This is tricky to do with Word's Find object (esp. with an array of search text).
    A more intuative approach might be to iterate throuogh the Words collection of each paragraph and test each one against the array.
    I'm posting in VBA here so other interested parties can use it - I'm sure you'll get the idea...

    A couple of points worth noting:
    With the Word Object Model, items from collections (like 'Paragraph', 'Word' and 'Character') return a range object
    The Trim function used below returns the string minus any leading/trailing spaces[VBA]Sub FindFromArray()

    Dim myDoc As Document
    Dim myRange As Range
    Dim myArray(1 To 3) As String
    Dim i As Integer, a As Integer
    Dim w As Range
    Dim strText As String

    Set myDoc = ActiveDocument
    myArray(1) = "Red"
    myArray(2) = "Green"
    myArray(3) = "Blue"

    For i = 1 To myDoc.Paragraphs.Count
    Set myRange = myDoc.Paragraphs(i).Range
    For Each w In myRange.Words
    For a = 1 To 3
    If Trim(w.Text) = myArray(a) Then
    strText = strText & myArray(a) & " found in paragraph " & i & Chr(13)
    End If
    Next
    Next
    Next
    MsgBox strText

    End Sub[/VBA]
    K :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •