Execute Method of Find Object

J

Joanne

Could someone clarify what the difference is between
..Execute (Replace:=wdReplaceall)
and
..Execute Replace:=wdReplaceall
with the "Find" object. I know this is a stupid question but I'm not sure
when to use the () and when not to.

Thank you very much.
 
J

Jay Freedman

Joanne said:
Could someone clarify what the difference is between
.Execute (Replace:=wdReplaceall)
and
.Execute Replace:=wdReplaceall
with the "Find" object. I know this is a stupid question but I'm not
sure when to use the () and when not to.

Thank you very much.

It is *not* a stupid question. It's a consequence of a stupid design
decision by the programmers who created VB and VBA.

The article at http://word.mvps.org/faqs/macrosvba/BracketsWithArgmnts.htm
explains when to use or not use parentheses for functions and subroutines in
general. In the specific case you're asking about, .Execute can be used as
*either* a function (it returns a Boolean true/false value saying whether or
not it found the search text) *or* a subroutine (you don't want the value,
you just want to search).

When you assign the result to a variable

bFound = .Execute(Replace:=wdReplaceall)

or when you use the implied result as a condition in an If statement

If .Execute(Replace:=wdReplaceall) Then

you use the parentheses. When you just fire off the search, you don't.

The stupid part is that the VBA interpreter can tell whether you're using
the result or not, because it complains when you do it wrong -- so why
should it care?? Just do it...
 
J

Joanne

I'm assuming that I wasn't too clear in my original message. I am debugging
some code that someone else wrote. In some areas of the code she used the ()
with .execute and in some areas she didn't. If I remove the (), I get an
error but I'm not sure why. I don't understand if there is a functional
difference in using parentheses or not since the rest of the .Execute
statement is the same.
 
J

Joanne

Thank you so much. That's the second time you've really clarified something
for me. I really appreciate it.
 

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