0
我想检查一下.txt文件是否退出。将文件设置为VBA中的文件对象
如果是这样,我想写入它。
如果不是,我想创建它并写入它。
我的问题是如果文件存在我不知道如何设置fso对象到文件,以便我可以写入它。你能帮我吗?
Sub WriteToTxtFile()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Dim FilePath As String
FilePath = "C:\Users\me\Desktop\file1.txt"
If Len(Dir(DirFile)) = 0 Then 'if the file does not exist create it
Set oFile = fso.CreateTextFile(FilePath)
oFile.WriteLine "# , Date, Open, High, Low, Close, Volume, VWAP"
oFile.WriteLine "1/1/2015, 10,10,10,10,10,10"
Else
Set oFile = ???.(FilePath) 'i know the file exists but what do I put here to access it?
oFile.WriteLine "# , Date, Open, High, Low, Close, Volume, VWAP"
oFile.WriteLine "1/1/2015, 10,10,10,10,10,10"
End If
oFile.Close
Set fso = Nothing
Set oFile = Nothing
End Sub
https://msdn.microsoft.com/en-us/library/aa242706%28v=vs.60%29的.aspx – 2015-03-13 17:32:14