excel 未在路径中创建指定的目录

a1o7rhls  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(112)
Option Explicit

Private log As Object
Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As LongPtr) As LongPtr
Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
Declare PtrSafe Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As LongPtr) As LongPtr
Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr
#If VBA7 Then
 Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
 Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds as Long) 'For 32 Bit Systems
#End If

'===================================== Log ====================================
Public Sub initLog()
On Error Resume Next
    Dim FSO As Object
    Dim sLogName As String
    Dim sLogPath As String
    Set FSO = CreateObject("Scripting.FileSystemObject")
    sLogPath = "C:\Users\" & UserName & "\Documents\Tmp\xlLog"
    sLogName = "\Metric-Log.txt"
    If Not FSO.FolderExists(sLogPath) Then
        MkDir sLogPath
        If Dir(sLogPath & sLogName) <> "" Then
            Set log = FSO.OpenTextFile(sLogPath & sLogName, 8, 0)
        Else
            Set log = FSO.CreateTextFile(sLogPath & sLogName, False)
        End If
    End If
    Set FSO = Nothing
End Sub

UserName在其他地方定义。但是没有创建\Tmp\xlLog。代码确实编译并运行过此函数,但是在文件资源管理器中查找时,没有显示具有sLogPath中指定名称的文件夹。

zpf6vheq

zpf6vheq1#

我忽略了on Error Resume Next,正如@JeremyThompson所指出的,这帮助我意识到我不能用一个MkDir命令创建两个文件夹,这解决了问题。

相关问题