Find Object and What Happens When Not Found (Word XP)

C

Culichi

Mornin'

What can I include in a .Find series of code that will let me give an
alternative order if the searched-for text is not found? The search
takes place inside a "For...Each..." series that gets search text from a
table cell. So far, it looks like this:

For Each . . .

. . .

With DoSearch
.ClearFormatting
.MatchCase = True
.Wrap =wdFindAsk
.Execute FindText:=TCellText.Text ^p
End With

. . .

Next

I think I can give it an alternate set of orders if wdFindAsk dialog
appears, but I can't figure out how to reference a dialog that I don't
specifically ask for.

A better solution would be to turn .Wrap off, and just give it a command
to do something if the find is unsuccessful after one trip through the
document. Can anyone tell me how an If...Then... (or some other order)
can reference the action of getting an empty Find/Search?

Many Thanks! You you all are unbelievably helpful!

Kelley
 
E

Ed

Hi, Kelley. What I have done is to place the .Execute in the If statement:
With DoSearch
.ClearFormatting
.MatchCase = True
.Wrap =wdFindAsk
****> .Execute FindText:=TCellText.Text ^p*** ' removed from here

' and placed in the If here
If Selection.Find.Execute (FindText:=TCellText.Text ^p) = True Then
' Do This
Else
' Do That
End If

HTH
Ed
 
J

Jay Freedman

Hi, Kelley,

The missing bit of information is this: The .Execute method, besides firing
off a search of the range DoSearch, also returns a boolean value describing
its success: True if the search text was found, or False if it wasn't found.
So you can do something like this:

If .Execute(FindText:=TCellText.Text) Then
' do something [or nothing] when text was found
Else
' do something [or nothing] when text was not found
End If

I'm not sure what you mean by an "alternative order". Do you mean you want
it to search for something else?

Also, you need to know that when .Execute was successful, the range DoSearch
is automatically repositioned to cover the found text. If you intend to
continue searching from that point toward the end of the document, you need
to say DoSearch.Collapse Direction:=wdCollapseEnd before you call .Execute
again.

Finally, you should understand better what .Wrap does. It does *not* control
whether the search is repeated -- that's the job of the loop. It *does*
control what happens when the search reaches the end of the range DoSearch
without finding the search text. The value you have, wdFindAsk, causes a
message box to pop up, asking you whether to continue past the end of the
range. Another value, wdFindContinue, automatically lets it search beyond
the end of the range. The one you want is wdFindStop, which makes the search
stop at the end of the range without asking.

By the way, what the heck is that ^p at the end of your .Execute statement?
If it isn't a typo, it will cause a syntax error.
 
C

Culichi

Thanks to both Ed and Jay. Seems like a simple question but I just
couldn't get the syntax figured out.

Jay: That ^p was an attempt to make the search find both the text and
and a paragraph return at the end of the text string. I included for
this reason: The search string is an article title, and the search
document a set of articles. Occasionally, the title is found within
article text, and I don't want that. So, I was trying to make it find a
hard return at the end of the search string. As you noted, it didn't
work. I think that setting MatchCase to true might solve the problem,
but if either of you (or anyone else) can tell me how to put the hard
return into the search, I'd appreciate it.

Thanks,

Kelley


Jay said:
Hi, Kelley,

The missing bit of information is this: The .Execute method, besides firing
off a search of the range DoSearch, also returns a boolean value describing
its success: True if the search text was found, or False if it wasn't found.
So you can do something like this:

If .Execute(FindText:=TCellText.Text) Then
' do something [or nothing] when text was found
Else
' do something [or nothing] when text was not found
End If

I'm not sure what you mean by an "alternative order". Do you mean you want
it to search for something else?

Also, you need to know that when .Execute was successful, the range DoSearch
is automatically repositioned to cover the found text. If you intend to
continue searching from that point toward the end of the document, you need
to say DoSearch.Collapse Direction:=wdCollapseEnd before you call .Execute
again.

Finally, you should understand better what .Wrap does. It does *not* control
whether the search is repeated -- that's the job of the loop. It *does*
control what happens when the search reaches the end of the range DoSearch
without finding the search text. The value you have, wdFindAsk, causes a
message box to pop up, asking you whether to continue past the end of the
range. Another value, wdFindContinue, automatically lets it search beyond
the end of the range. The one you want is wdFindStop, which makes the search
stop at the end of the range without asking.

By the way, what the heck is that ^p at the end of your .Execute statement?
If it isn't a typo, it will cause a syntax error.
Mornin'

What can I include in a .Find series of code that will let me give an
alternative order if the searched-for text is not found? The search
takes place inside a "For...Each..." series that gets search text
from a table cell. So far, it looks like this:

For Each . . .

. . .

With DoSearch
.ClearFormatting
.MatchCase = True
.Wrap =wdFindAsk
.Execute FindText:=TCellText.Text ^p
End With

. . .

Next

I think I can give it an alternate set of orders if wdFindAsk dialog
appears, but I can't figure out how to reference a dialog that I don't
specifically ask for.

A better solution would be to turn .Wrap off, and just give it a
command to do something if the find is unsuccessful after one trip
through the document. Can anyone tell me how an If...Then... (or some
other order) can reference the action of getting an empty Find/Search?

Many Thanks! You you all are unbelievably helpful!

Kelley
 
J

Jay Freedman

Hi, Kelley,

I thought it might be something like that.

By itself, ^p doesn't mean anything to VBA. (Well, technically it does, but
has to do with numbers and exponents, and doesn't apply here.) You need to
attach a string containing those two characters onto the end of the string
containing the cell text. The operator for sticking two strings together is
&. Then you need quotes around the ^p to make it a string. The resulting
correct syntax is

FindText:=TCellText.Text & "^p"

--
Regards,
Jay Freedman
Microsoft Word MVP
Thanks to both Ed and Jay. Seems like a simple question but I just
couldn't get the syntax figured out.

Jay: That ^p was an attempt to make the search find both the text and
and a paragraph return at the end of the text string. I included for
this reason: The search string is an article title, and the search
document a set of articles. Occasionally, the title is found within
article text, and I don't want that. So, I was trying to make it find
a hard return at the end of the search string. As you noted, it didn't
work. I think that setting MatchCase to true might solve the problem,
but if either of you (or anyone else) can tell me how to put the hard
return into the search, I'd appreciate it.

Thanks,

Kelley


Jay said:
Hi, Kelley,

The missing bit of information is this: The .Execute method, besides
firing off a search of the range DoSearch, also returns a boolean
value describing its success: True if the search text was found, or
False if it wasn't found. So you can do something like this:

If .Execute(FindText:=TCellText.Text) Then
' do something [or nothing] when text was found
Else
' do something [or nothing] when text was not found
End If

I'm not sure what you mean by an "alternative order". Do you mean
you want it to search for something else?

Also, you need to know that when .Execute was successful, the range
DoSearch is automatically repositioned to cover the found text. If
you intend to continue searching from that point toward the end of
the document, you need to say DoSearch.Collapse
Direction:=wdCollapseEnd before you call .Execute again.

Finally, you should understand better what .Wrap does. It does *not*
control whether the search is repeated -- that's the job of the
loop. It *does* control what happens when the search reaches the end
of the range DoSearch without finding the search text. The value you
have, wdFindAsk, causes a message box to pop up, asking you whether
to continue past the end of the range. Another value,
wdFindContinue, automatically lets it search beyond the end of the
range. The one you want is wdFindStop, which makes the search stop
at the end of the range without asking.

By the way, what the heck is that ^p at the end of your .Execute
statement? If it isn't a typo, it will cause a syntax error.
Mornin'

What can I include in a .Find series of code that will let me give
an alternative order if the searched-for text is not found? The
search takes place inside a "For...Each..." series that gets search
text from a table cell. So far, it looks like this:

For Each . . .

. . .

With DoSearch
.ClearFormatting
.MatchCase = True
.Wrap =wdFindAsk
.Execute FindText:=TCellText.Text ^p
End With

. . .

Next

I think I can give it an alternate set of orders if wdFindAsk dialog
appears, but I can't figure out how to reference a dialog that I
don't specifically ask for.

A better solution would be to turn .Wrap off, and just give it a
command to do something if the find is unsuccessful after one trip
through the document. Can anyone tell me how an If...Then... (or
some other order) can reference the action of getting an empty
Find/Search?

Many Thanks! You you all are unbelievably helpful!

Kelley
 

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