PDA

View Full Version : Solved: Searching Document Text w/Tables



danvanf
07-09-2008, 08:02 PM
I am using VBA from Word Document, attempting to search documents for a particular text string.("<ForEach")
Then grab the text Right of the found Text and do something with it.
Everything is working fine, except when my document contains tables. Then when "<ForEach" is found in a table cell my code gets stuck there.

Since the text ForEach actually means ForEach to another interpreter they also close somewhere.

The following code is used to count "<ForEach"'s there's a similar part to count closes all to current cursor position. This code gets stuck when the Text is found in a table



Do While StayIn
With Selection.Find
.Forward = True
.MatchWholeWord = True
.MatchCase = True
.Text = "<FOREACH"
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
CountForEach = CountForEach + 1
Selection.Start = Selection.End 'Make the ending of the currently found text the new start position
Selection.End = Selend ' Selend is the initial cursor position
Else
StayIn = False
End If
Loop

The code continues to count closes, then move to the correct open ForEach, and returns the text to the right. Those sections look remarkably similar to this one.

Document Looks like this

<ForEach Camel> ride it
<ForEach Bird> watch it
<ForEach Cow> milk it
</ForEach >
</ForEach > Do something else with the camel
</ForEach >
I need to be able to click anywhere and figure out which ForEach is valid.
A constant is if a ForEach opens in a table cell, it must close in that table cell.
They can wrap tables, text, or graphics, I don't think Word based Text Boxes are an issue, I suppose I should ignore them if present, and also suppose that's another thread.

Thanks for any assistance
Dan

danvanf
07-12-2008, 09:25 PM
Issue solved by placing Selection.Text into a string variable, then work with that variable, rather than the actual document. I also switched to Regular Expressions to find and note the positions of the search items in an array.

Dan