|
《经典代码应用大全的》15.1章和15.2章分别用Open语句配合write和print语句 及用FSO对象创建一个文本文档并写入内容,完成的工作基本一样,请问这两种方法是有什么区别吗?应用的时候该怎么取舍呢?
15.1章Open语句代码
Sub 15.1WriteFile()
Dim intNum As Integer
intNum = FreeFile
Open ThisWorkbook.Path & "\test.txt" For Output As #intNum
Write #intNum, "Hello "; "ExcelHome"
Write #intNum, "这是一个测试文件"
Write #intNum,
Write #intNum, "以上是由Write #语句写入的数据"
Print #intNum, "这是一个测试文件"
Print #intNum, "Hello "; "ExcelHome"
Print #intNum,
Print #intNum, Spc(5); "VBA程序开发版"
Print #intNum, Tab(10); "VSTO程序开发版"
Close intNum
End Sub
15.2章FSO对象代码
Sub WriteFile()
Dim objFso As Object
Dim objFile As Object
Dim objStream As Object
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objStream = objFso.CreateTextFile(ThisWorkbook.Path & "\fsotest1.txt", True)
With objStream
.WriteLine ("这是第一个测试文件")
.WriteBlankLines (1)
.Write ("Hello ")
.Write ("ExcelHome")
.WriteLine
.WriteLine (Space(5) & "VBA程序开发版")
.Close
End With
Set objStream = objFso.OpenTextFile(ThisWorkbook.Path & "\fsotest2.txt", 2, True)
objStream.WriteLine ("这是第二个测试文件")
objStream.Close
objFso.CreateTextFile (ThisWorkbook.Path & "\fsotest3.txt")
Set objFile = objFso.GetFile(ThisWorkbook.Path & "\fsotest3.txt")
Set objStream = objFile.OpenAsTextStream(2, 0)
objStream.WriteLine ("这是第三个测试文件")
objStream.Close
Set objFile = Nothing
Set objStream = Nothing
Set objFso = Nothing
End Sub
|
|