Find get's stuck in table cells

F

Felix Dejavu

Platform: Win98se, MSOffice97

I'm trying to count the number of words in a document that have a given
style. However, when I do a Find with FindText empty (""), the
Find gets stuck in table cells. Here is an example of the strange
behavior:

======== Sample code 1

Public Sub StyleSearch()
Dim f as Find
Set f = ActiveDocument.Range(End:=0).Find

On Error Goto StyleMissing
f.style = ActiveDocument.Styles("MyStyle")
On Error Goto 0
do While (f.Execute(FindText:="", Forward:=True, Wrap:=wdFindStop,
Format:=True))
MsgBox f.Parent.Text
Loop
Done:
Exit Sub

StyleMissing:
MsgBox "Style does not exist in document"
Resume Done
End Sub

======== Sample document (Assume all text is of the MyStyle style)

Ali
+-----+-----+
|Bob |Carl |
+-----+ +
|Dan | |
+-----+-----+
Edward

========


If I run this code on the document, it will output:
Ali
Bob
Bob
Bob
Bob
....

and never get past the first cell. I figured it just needed a little push,
so I added a line to make sure it kept advancing:

======== Sample code 2

Public Sub StyleSearch()
Dim f as Find
Set f = ActiveDocument.Range(End:=0).Find

On Error Goto StyleMissing
f.style = ActiveDocument.Styles("MyStyle")
On Error Goto 0
do While (f.Execute(FindText:="", Forward:=True, Wrap:=wdFindStop,
Format:=True))
MsgBox f.Parent.Text
f.Parent.Start = f.Parent.End 'HERE IS THE FIX I TRIED
Loop
Done:
Exit Sub

StyleMissing:
MsgBox "Style does not exist in document"
Resume Done
End Sub

========

Running this causes the output:
Ali
Bob
Carl
Dan
Carl
Dan
Carl
Dan
....

and never gets past the large cell. Apparently messing with the Range's
Start and End during a search is not a good idea. I monitored the range
indicies and it will actually jump backward even though the search is
forward. Not only that but this fix causes the code to hang at the end of
the document (if it ever gets there).

This is only a problem because I am searching with an empty text string,
but that is essential to my algorithm unless I abandon Find altogether. Is
there any way I can get my Find to make it through a table without
hanging?!

thanks for reading and for any insights you may have,
-fd
 
D

Doug Robbins - Word MVP

Hi Felix,

The following is a bit of a kludge, but it seems to work:

Dim Counter As Integer
Counter = 0
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Emphasis")
With Selection.Find
Do While .Execute(FindText:="", MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Counter = Counter + 1
Selection.MoveRight wdWord
Loop
End With
If Counter = 0 Then
MsgBox "The Style is not used in the document."
Else
If Selection.Information(wdWithInTable) = True Then Counter =
Counter - 1
MsgBox "There are " & Counter & " instances of the style in the
document."
End If

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top