作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个文本文件并向其中写入数据,很简单。好吧,它不起作用,我到处寻找它但找不到答案。
当它到达 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/
我是一名优秀的程序员,十分优秀!