Check if file exists...

G

Guest

I use the following to save a file to folder. How can I have it check
to see if the filepath exists and it exits then msgbox indicated that file
is there and exit sub routine, and if not exists, go ahead and create the
path?

ActiveWorkbook.SaveAs Filename:="\\my path directory\" & NewFileName & ".xls"
 
G

Guest

try something like this:

If Dir("\\my path directory\" & NewFileName & ".xls") = "" Then
ActiveWorkbook.SaveAs Filename:="\\my path directory\" & NewFileName &
".xls"
Else
MsgBox "File already exists!"
Exit Sub
End If
 
N

NickHK

Not sure if you are talking about file or path to exist. You have some
suggestions already, but here's another.
You can use the Windows API to check/create a path:

<API-Guide>
Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal
lpPath As String) As Long

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error
information, call GetLastError.
</API-Guide>

NickHK
 

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