大概的代码,请参考: Option Explicit Sub test() Dim w1 As String w1 = CreatePath("d:\fg\fg\fg") If w1 <> "" Then MsgBox w1 End Sub Function CreatePath(ByVal bPath As String) As String CreatePath = "请输入正确的路径!" ''''给一个默认的错误信息 bPath = Trim(bPath) If Len(bPath) = 0 Then Exit Function bPath = bPath & "\" On Error GoTo errs Dim s1 As String, i As Integer, s2 As String, pp pp = Split(bPath, "\") If UBound(pp) > 1 Then s1 = pp(0) For i = 1 To UBound(pp) - 1 s2 = Trim(pp(i)) If s2 = "" Then Exit Function s1 = s1 & "\" & s2 If Dir$(s1, vbDirectory) = "" Then MkDir s1 Next End If CreatePath = "" Exit Function errs: CreatePath = Err.Description End Function ...
建文件夹是MKDIR
Option Explicit
Sub test()
Dim w1 As String
w1 = CreatePath("d:\fg\fg\fg")
If w1 <> "" Then MsgBox w1
End Sub
Function CreatePath(ByVal bPath As String) As String
CreatePath = "请输入正确的路径!" ''''给一个默认的错误信息
bPath = Trim(bPath)
If Len(bPath) = 0 Then Exit Function
bPath = bPath & "\"
On Error GoTo errs
Dim s1 As String, i As Integer, s2 As String, pp
pp = Split(bPath, "\")
If UBound(pp) > 1 Then
s1 = pp(0)
For i = 1 To UBound(pp) - 1
s2 = Trim(pp(i))
If s2 = "" Then Exit Function
s1 = s1 & "\" & s2
If Dir$(s1, vbDirectory) = "" Then MkDir s1
Next
End If
CreatePath = ""
Exit Function
errs:
CreatePath = Err.Description
End Function
...