PDA

View Full Version : Search for multiple words



jakesp
04-25-2005, 03:28 AM
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

Killian
04-25-2005, 05:27 AM
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

jakesp
04-25-2005, 05:45 AM
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");
}
}
}

TonyJollans
04-25-2005, 06:14 AM
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.

Killian
04-25-2005, 06:34 AM
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 spacesSub 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