Best way to check if doc exists?

E

Ed

I know there are a couple of different ways to see if a document exists
(before I throw an error trying to do something with it!), but I'm wondering
if there is a "best" way - that is, one most likely to behave nicely in just
about any routine I stick it into. The path will always be the same on any
user's computer - with their username inserted, of course - so I could use
that. I've done different things - some quite by accident - in other code:
set an object to a doc that isn't there and trap the lack of an object name;
try to open it and trap the error; and such like. I tried Exists, but that
doesn't seem to work for documents. Any suggestions?

Ed
 
J

Jay Freedman

Hi Ed,

One way is to feed the desired path and filename into the Dir() function and
look at the returned value. If it's an empty string, the file doesn't exist.
Example:

If Dir$(myPathAndName) = "" Then
' it doesn't exist
 
E

Ed

Thanks, Jay. This may be the first time I've purposely made an empty string
part of my code! 8>)

Ed
 

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