gpt4 book ai didi

vba - 创建文件时找不到路径-VBA

转载 作者:行者123 更新时间:2023-12-03 02:04:39 24 4
gpt4 key购买 nike

我正在尝试创建一个文本文件并向其中写入数据,很简单。好吧,它不起作用,我到处寻找它但找不到答案。

当它到达 CreateTextFile() 方法时,它会抛出路径未找到错误。但我已确保该路径有效且存在。

'Create a text file
Private Sub OpenFile()
Dim filePath As String
Dim fileName As String
Dim fullPath As String
Const ForAppending = 8, TristateFalse = 0
Dim curDate As Date
Dim strDate As String
curDate = Date
strDate = CStr(curDate)
fileName = "DCSSInputLitigation_" & "(" & strDate & ")" & ".txt"

filePath = "C:\TempFolder\"

Set fs = CreateObject("Scripting.FileSystemObject")
fullPath = fs.BuildPath(filePath, fileName)

Set fWriter = fs.CreateTextFile(fullPath)

End Sub

当我在方法中对路径进行硬编码时,它可以工作,但当我使用变量时则不行。有什么想法吗?

  Set fWriter = fs.CreateTextFile("C:\TempFolder\test.txt")

最佳答案

当您获得如下日期时:

strDate = CStr(curDate)

您正在将 / 添加到文件名中,您创建的 fullPath 的字符串值为:

C:\TempFolder\DCSSInputLitigation_(6/12/2014).txt

在 Windows 上,文件名中不能包含 /,因此您会遇到问题。

您可以设置日期格式或替换 /,例如:

strDate = replace(CStr(curDate),"/","-")
strDate = Format(curDate,"dd-mm-yyyy")

两者都可以。

关于vba - 创建文件时找不到路径-VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24192933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com