VBA 使用FileSystemObject写入现有文件
示例
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
热门推荐
7 十月简短祝福语
10 新疆新年祝福语大全简短
11 参加儿子大学祝福语简短
12 青春告白祖国简短祝福语
13 新年祝福语短句简短搞笑
14 周末祝福语正能量简短
15 长辈钻石婚祝福语简短
16 师生聚餐的祝福语简短
17 简短的祝福语12字
18 同事男生节祝福语简短