How to make a folder and subfolder with `FileSystemObject.CreateFolder` at the same time

518 Views Asked by At

Top macro works, bottom one doesn't unless I run the top one first. How do I create folder & subfolder concurrently?

I'm trying to use FileSystemObject.CreateFolder to make a folder and subfolder at the same time. The top macro works and the bottom one dosen't. The bottom macro will successfully run if I run the top one first.

Go ahead guys, make me look like an idiot.

Sub MakeDir1() ' this macro works
    Dim DocPath As String
    DocPath = "E:\@Workorders\test\"  ' E:\@Workorders\ is an existing folder
    
    Dim FSO As FileSystemObject
    Set FSO = New FileSystemObject
    If Not FSO.FolderExists(DocPath) Then
        FSO.CreateFolder (DocPath)
    End If
End Sub
Sub MakeDir2() ' this macro throws an error at "FSO.CreateFolder (DocPath)"
    Dim DocPath As String
    DocPath = "E:\@Workorders\test\test2\"
    
    Dim FSO As FileSystemObject
    Set FSO = New FileSystemObject
    If Not FSO.FolderExists(DocPath) Then
        FSO.CreateFolder (DocPath)
    End If
End Sub
1

There are 1 best solutions below

2
user1636620 On

This works:

Sub MakeDir3() ' this macro works

Dim DocPath As String, DocFolder1 As String
    DocPath = "E:\@Workorders\test\"
    DocFolder1 = "test2"
    
Dim FSO As FileSystemObject
    Set FSO = New FileSystemObject
        If Not FSO.FolderExists(DocPath) Then
            FSO.CreateFolder (DocPath)
            FSO.CreateFolder (DocPath & DocFolder1)
        End If

End Sub