示例
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Sub WriteTextFileExample()
Dim oFso
Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile as Object
Dim myFilePath as String
Dim myFileText as String
myFilePath = "C:\mypath\to\myfile.txt"
' First check if the file exists
If oFso.FileExists(myFilePath) Then
' this will overwrite any existing filecontent with whatever you send the file
' to append data to the end of an existing file, use ForAppending instead
Set oFile = oFso.OpenTextFile(myFilePath, ForWriting)
Else
' create the file instead
Set oFile = oFso.CreateTextFile(myFilePath) ' skipping the optional boolean for overwrite if exists as we already checked that the file doesn't exist.
End If
oFile.Write"This is some new text"
oFile.Write" 该文本将在文本的第一位之后出现。"
oFile.WriteLine"这段文字包括一个换行符,以确保每次写入都使用自己的行。"
oFile.Close' close the file
End Sub