Checking to see if a file exists.

J

JHB

I would like to create a macro that will check to ensure that a file
exists, which will have a material efefct on the steps to be taken
next. I am havintg trouble finding out how to do this. Can someone
tell me the command argument for an if command that would make the
check, as if:

IF Exists xxx.xls then
else
endif

Thanks a lot.

John Baker
 
D

Dave Peterson

Dim TestStr as string
teststr = ""
on error resume next
teststr = dir("Yourpathtothefile\yourfilename.ext")
on error goto 0

if teststr = "" then
'not there
else
'yes, it is!
end if
 
E

Eduardo

Hi,
this should get you started:

Function FileExists(fName) As Boolean
' True if exists
On Error GoTo FileExists_Error

FileExists = False

Dim x As String
x = Dir(fName)
If x <> "" Then FileExists = True

Exit Function

FileExists_Error:
Select Case Err.Number
Case 52
FileExists = False
Case Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure FileExists "
End Select
End Function
 
J

JHB

Hi,
this should get you started:

Function FileExists(fName) As Boolean
' True if exists
    On Error GoTo FileExists_Error

    FileExists = False

    Dim x As String
    x = Dir(fName)
    If x <> "" Then FileExists = True

     Exit Function

FileExists_Error:
    Select Case Err.Number
    Case 52
        FileExists = False
    Case Else
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure FileExists "
    End Select
End Function

Thank you both very much. I am a neophyte, and these answers are a
great help!

John Baker
 

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