After my signature is some code that I have posted in the past to the
compiled VB newsgroups, but which will work fine in Excel VBA as well. Note
that it is presented as a Function which means you can check if it worked
okay or not using a structure like this...
If MakeDirectoryPath("c:\dir1\dir2\dir3\dir4") Then
MsgBox "Directory path created successfully"
Else
MsgBox "An error occurred while trying to create that directory"
End If
allowing you to take any steps necessary for either case. Or, if you want to
omit the check, you can call it directly as if it were a subroutine (you can
do this for any function) using either this...
MakeDirectoryPath "c:\dir1\dir2\dir3\dir4"
or this...
Call MakeDirectoryPath("c:\dir1\dir2\dir3\dir4")
Rick
Here is a subroutine (VB6) that will create of all the sub-directories (that
do not already exist) for a specified path argument. The error checking was
made up off the top of my head, but I think it covers the problems that one
might encounter. Note that the path must start with a drive letter, followed
by a colon, followed by a backslash, followed by the directories path.
Function MakeDirectoryPath(ByVal Path As String) As Boolean
Dim X As Long
Dim NewPath As String
Dim Parts() As String
On Error GoTo OOPS
If Path Like "[a-zA-Z]:\*" And InStr(Path, "\\") = 0 Then
If Len(Dir$(Left$(Path, 3))) = 0 Then Exit Function
Parts = Split(Path, "\")
Parts(0) = Parts(0) & "\"
NewPath = Parts(0)
For X = 0 To UBound(Parts)
If Len(Dir$(NewPath, vbDirectory)) = 0 Then MkDir NewPath
If Right$(NewPath, 1) <> "\" Then NewPath = NewPath & "\"
If X < UBound(Parts) Then NewPath = NewPath & Parts(X + 1)
Next
MakeDirectoryPath = True
End If
OOPS:
End Function
Barb Reinhardt said:
I'd like to test for a folder path (Z:\FolderName) and if it doesn't
exist,
I'd like to create it. Can someone provide a snippet of code on how to do
this?
Thanks,
Barb Reinhardt