Open App.Path & "\Folder\" & str(0) For Output
似乎找不到路径但是如果在此之前我确实找到了
MsgBox App.Path & "\Folder\" & str(0)
它提供了我想要的正确目录/文件名
如果我用引号中的直接路径替换该字符串,它可以正常工作,但是这对我的应用程序的其他用户来说不是很好:(有人知道为什么这不起作用吗?
您可以打开一个不存在的文件。我试过了:
Open "c:\temp\test.txt" & Str(0) For Output As #1
Close #1
当它运行它创建 c:\temp\test.txt 0
请注意,我在 Open 语句中添加了“As #1”,并且 Str(0) 为可选的减号添加了前导空格(CStr(0) 不添加前导空格)
注释:您可以打开一个不存在的文件。
仅当您的文件夹存在时才为真。如果您的文件夹和文件都不存在,则会出现“找不到路径”错误。
这是我为你做的一些简单的事情:
Function CreateLog(Destination As String, MyMessage As String)
Dim PathToCreate, FolderPath, FileName As String
'Check for Unnecessary Spaces
Destination = Trim(Destination)
FolderStr = Destination
'Gather only FolderPath of Destination
Do
FolderStr = Mid(FolderStr, 1, Len(FolderStr) - 1)
Loop Until Right(FolderStr, 1) = "\" Or Len(FolderStr) < 1
'Gather only FileName
FileName = Mid(Destination, Len(FolderStr) + 1, Len(Destination) - Len(FolderStr))
'If the path does not exist than create it
'Recursive approach
For Each Folder In Split(FolderStr, "\")
If InStr(1, Folder, ":") = 0 Then
PathToCreate = PathToCreate & "\" & Folder
Else
PathToCreate = Folder
End If
If fso.FolderExists(PathToCreate) = False And PathToCreate <> "" Then
fso.CreateFolder PathToCreate
End If
Next
'Open file and add the message in it
Open PathToCreate & "\" & FileName & ".txt" For Append As #1
Print #1, MyMessage
Close #1
End Function
用法:
CreateLog "D:\Test\NewTest\NewFolder\AnotherFolder\atlastthefile.abcdefg", "Hello!"
不管给出什么文件扩展,无论如何都会添加“.txt”。