0

顶部宏有效,底部宏不起作用,除非我先运行顶部宏。如何同时创建文件夹和子文件夹?

我正在尝试同时FileSystemObject.CreateFolder创建一个文件夹和子文件夹。顶部宏有效,底部宏无效。如果我先运行顶部宏,底部宏将成功运行。

来吧,伙计们,让我看起来像个白痴。

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
4

1 回答 1

0

这有效:

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
于 2021-09-16T22:03:11.780 回答