テキストファイル入力
かつての方式
Dim strFile As String
Dim strLine As String
Dim num = FreeFile
strFile = "d:\work\testfile.txt"
Open strFile For Input As #num
Do Until EOF(num)
Line Input #num, strLine
....
Loop
Close #num
よりモダンな方式
Dim fso As Object
Dim rf As Object
Dim strFilePath as String
Dim strLine As String
strFilePath = "d:\work\testfile.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set rf = fso.OpenTextFile(strFilePath, ForReading, False)
Do While rf.AtEndOfStream <> True
strLine = rf.ReadLine
...
Loop
rf.Close
Set fso = Nothing
テキストファイル出力
かつての方式
Dim strFilePath as String
Dim strLine As String
Dim num = FreeFile
strFilePath = "d:\work\testfile.txt"
strLine = "Hello world!"
Open strFilePath For Append As #num
Print #num, strLine
Close #num
よりモダンな方式
Dim fso As Object
Dim af As Object
Dim strFilePath as String
Dim strLine As String
strFilePath = "d:\work\testfile.txt"
strLine = "Hello world!"
Set fso = CreateObject("Scripting.FileSystemObject")
Set af = fso.OpenTextFile(strFilePath, ForAppending, True)
af.Write strLine
af.Close
Set fso = Nothing