How to determine if a Folder/Directory Exists in Excel VBA

R

robinson.william

Hi All,

Does anybody know how i can determine if a directory exists in VBA? For
example i would like to write a function that looks to see if the path
c:\reports\2006 exists, if so leave it be, if not create it.

Thanks,
Bill
 
D

Dave Peterson

Why bother checking. Just ignore any errors that occur when you try to create
it:

on error resume next
mkdir "C:\reports"
mkdir "C:\reports\2006"
on error goto 0

(One level at a time.)
 
N

NickHK

Bill,
Dave's answer is native VBA, but there's also the API route:

<From API-Guide: URL: http://www.allapi.net/>
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll"
(ByVal lpPath As String) As Long

Private Sub Form_Load()
'create the directory "c:\this\is\a\test\directory\", if it doesn't exist
already
MakeSureDirectoryPathExists "c:\this\is\a\test\directory\"

End Sub
</From 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

Similar Threads


Top