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