Find function always returns true.

S

sean

Hi All.

I am using a custom SearchDocument function that receives the range and
search string as the parameters, and the function returns a Boolean. The
problem is that the function is always returning a true value.

The code is listed below:

Function SearchDocument(oRange As Range, sFind As String) As Boolean

Dim boolResult As Boolean



With oRange.Find

.ClearFormatting

.Forward = True

.MatchAllWordForms = False

.MatchCase = False

.MatchSoundsLike = False

.MatchWholeWord = False

.MatchWildcards = True

.Wrap = wdFindStop

.Text = sFind

.Execute

boolResult = .Found

End With

'MsgBox boolResult

SearchDocument = boolResult

End Function



If I pass the following search string:



"^13[! ]@.ZIP*^13[! ]@.ZIP"



The range that is being passed is the remaining content of the document.
Everytime I find the range, I move the end of the range back by one
paragraph to insure that I get the correct information. Then I copy the
range into another file and then delete everything but the line that
contains the .zip. The Output looks like:



ALLCOSTS.ZIP AT ALL COSTS

ARTFULDG.ZIP SEALS--TOP SECRET OPERATION: ARTFUL DODGER

BALPOWR1.ZIP Balance of Power

By: Tom Clancy

No synopsis available.



<end of document>





Why isn't the find failing when it gets to "BALPOWR1.ZIP Balance of Power"?
There is no more text with ".zip" starting at the beginning of the line.



Confused, Sean
 
C

Cindy Meister -WordMVP-

Hi Sean,

I'm not sure...

When I paste your sample text into a document, then use the search string,
I have to click *4* times until it actually highlights anything. This makes
me wonder if you aren't running into a "glitch" where Word is only able to
find a partial match. Look up the Find/Replace stuff on mvps.org/word from
Bill Coan and see if any of those rings a bell (like "bad karma")

The other thing to watch out for is that you're actually resetting the
range before running the search function, again. You don't show us the code
you're using for that.
I am using a custom SearchDocument function that receives the range and
search string as the parameters, and the function returns a Boolean. The
problem is that the function is always returning a true value.

The code is listed below:

Function SearchDocument(oRange As Range, sFind As String) As Boolean

Dim boolResult As Boolean



With oRange.Find

.ClearFormatting

.Forward = True

.MatchAllWordForms = False

.MatchCase = False

.MatchSoundsLike = False

.MatchWholeWord = False

.MatchWildcards = True

.Wrap = wdFindStop

.Text = sFind

.Execute

boolResult = .Found

End With

'MsgBox boolResult

SearchDocument = boolResult

End Function



If I pass the following search string:



"^13[! ]@.ZIP*^13[! ]@.ZIP"



The range that is being passed is the remaining content of the document.
Everytime I find the range, I move the end of the range back by one
paragraph to insure that I get the correct information. Then I copy the
range into another file and then delete everything but the line that
contains the .zip. The Output looks like:



ALLCOSTS.ZIP AT ALL COSTS

ARTFULDG.ZIP SEALS--TOP SECRET OPERATION: ARTFUL DODGER

BALPOWR1.ZIP Balance of Power

By: Tom Clancy

No synopsis available.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
K

Klaus Linke

Hi Sean,

Like Cindy, I'm curious how you set the new Range to search to the rest of
the document.

It might be easiest to make oRange a public variable (which is available in
all macros of the module) by DIMming it in the declaration section at the
top of the module.

You could then start by setting
oRange=ActiveDocument.Content
and then putting some
oRange.Select
in your code to watch what gets selected.

As Cindy also said, you will run into the "bad karma" bug:
Since your search text contains ".ZIP" twice, it will finally start
matching the last ".ZIP" and then croak with a partial match.
..Found will falsely return "TRUE", and oRange.End will be zero.

You can take care of that if you replace
boolResult = .Found
with
boolResult = .Found And Not (oRange.End = 0)

The part in your sample that starts with "BALPOWR1.ZIP Balance of Power" up
to the <end of document> can not be matched by your search expression,
because it doesn't contain a second ".ZIP" line at the end.

After the last successfull "Find", you would have to treat the rest of the
document as a special case.

The code pasted below seemed to work.
I left some "oRange.Select" and MessageBoxes in it for
debugging/single-stepping.

Greetings,
Klaus




Option Explicit
Dim oRange As Range

Sub TestSearchDocument()
Set oRange = ActiveDocument.Content
While SearchDocument("^13[! ]@.ZIP*^13[! ]@.ZIP") = True
oRange.MoveEnd Unit:=wdParagraph, Count:=-1
oRange.Select
MsgBox "match!"
oRange.Collapse (wdCollapseEnd)
oRange.End = ActiveDocument.Content.End
Wend
oRange.End = ActiveDocument.Content.End
oRange.MoveStart Unit:=wdParagraph, Count:=-1
oRange.Select
MsgBox "Game, set and match!"
End Sub

Function SearchDocument(sFind As String) As Boolean
With oRange.Find
.ClearFormatting
.Forward = True
.MatchAllWordForms = False
.MatchCase = False
.MatchSoundsLike = False
.MatchWholeWord = False
.MatchWildcards = True
.Wrap = wdFindStop
.Text = sFind
.Execute
oRange.Select
SearchDocument = .Found And Not (oRange.End = 0)
End With
End Function
 

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