Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 260
cAlternate As String * 14
End Type
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Function mypath(path As String) As String
If Right(path, 1) = "\" Then
mypath = Left(path, Len(path) - 1)
Else
mypath = path
End If
End Function
在你要实现添加目录的sub 或 function 中添加以下代码:
Dim hSearch As Variant
Dim attr As SECURITY_ATTRIBUTES
Dim filedata As WIN32_FIND_DATA
hSearch = FindFirstFile(mypath(App.path) + "\目录名", filedata)
If (hSearch = INVALID_HANDLE_VALUE) Then
If CreateDirectory(mypath(App.path) + "\目录名", attr) = 0 Then
debug.print "error"
End If
Else
FindClose hSearch
End If