testing for file(s)

S

scott

hi

I've modified some VBA for office code to fetch pricing details from a file
instead of being embedded in the body of the code iteslf (the code is
embedded into a word .dot file). now when they change their prices, they can
update the pricing file instead of calling me in (doing myself out of a job,
but it saves a journey in !!)

I'm not quite sure how to handle the code if they ever delete/move/corrupt
the pricing file ! currently, if the file is unreachable for any reason, the
code will just crap out

I looked for an 'exists' function, but it only applies to bookmarks and
tasks, not files

I presume I need to just try and open the file, and then handle what it
returns in the form of a sucess/fail result

can anyone confim this, or is there some clever function for doing this sort
of thing ? office version is xp (2002 ?)

many thanks

_scott
 
P

Peter Hewett

Hi scott

The easy way is:

Public Function FileExists(ByVal strFile As String) As Boolean
FileExists = LenB(Dir$(strFile)) > 0
End Function

call it like this:

If FileExists("c:\mydir\myfile.txt") Then
' Exists
Else
' Does not exist
EndIf

HTH + Cheers - Peter
 

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