'创建文本并写入记录 - 每次运行会替换原有的文件
'以下三种方法任选其一,都可以正常使用
'---------------------方法一、--------------------------------
Function WriteFile(filepath As String, str As String)
Dim objFso As Object
Dim objStream As Object
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objStream = objFso.CreateTextFile(filepath, True)
With objStream
.WriteLine str
.Write str
.Close
End With
Set objStream = Nothing
Set objFso = Nothing
End Function
'---------------------方法二、--------------------------------
Function WriteFile2(filepath As String, str As String)
Dim objFso As Object
Dim objStream As Object
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objStream = objFso.OpenTextFile(filepath, 2, True)
objStream.WriteLine (str)
objStream.Close
Set objStream = Nothing
Set objFso = Nothing
End Function
'---------------------方法三、--------------------------------
Function WriteFile3(filepath As String, str As String)
Dim objFso As Object
Dim objFile As Object
Dim objStream As Object
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CreateTextFile (filepath)
Set objFile = objFso.GetFile(filepath)
Set objStream = objFile.OpenAsTextStream(2, 0)
objStream.WriteLine (str)
objStream.Close
Set objFile = Nothing
Set objStream = Nothing
Set objFso = Nothing
End Function
'打开现有文本并写入记录 - 每次运行在原有的基础上写入记录
'文本不存在时,也会创建新的文本并写入记录
Function AppendFile(filepath As String, str As String)
Dim objFso As Object
Dim objStream As Object
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objStream = objFso.OpenTextFile(filepath, 8, True)
objStream.WriteLine str
objStream.Close
Set objStream = Nothing
Set objFso = Nothing
End Function
'打开文本并读取记录
Function ReadFile(filepath As String, str As String) As String
Dim objFso As Object
Dim objStream As Object
Dim strTemp As String
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objStream = objFso.OpenTextFile(filepath, 1)
ReadFile= objStream.ReadAll
objStream.Close
Set objStream = Nothing
Set objFso = Nothing
End Function