Read A Text File Using Windows Script Host
This example shows an alternate way to read text using the Windows Script Host objects instead of handles.
Program Code
Option Explicit
Public Sub ReadTextScript()
Dim objFSO As Object
Dim objTextStream As Object
Dim strTextLine As String
On Error GoTo HandleScriptError:
' ********************************************************
' Set Up File System Objects That Allow The Program
' To Read A Text File Sequentially
' ********************************************************
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.FileExists("C:\samplefile1.txt")) = False Then
MsgBox ("C:\samplefile1.txt Does Not Exist")
Set objFSO = Nothing
On Error GoTo 0
Exit Sub
End If
Set objTextStream = objFSO.OpenTextFile("C:\samplefile1.txt", 1)
Do While objTextStream.AtEndOfStream <> True
strTextLine = objTextStream.ReadLine
MsgBox ("Current Text Line = " & strTextLine)
Loop
ReadTextFileExit:
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
Exit Sub
HandleScriptError:
MsgBox "An Error Occurred In This Application" & vbCrLf & _
"Please Contact The Developer" & vbCrLf & vbCrLf & _
"Error Number = " & Err.Number & " Description = " & _
Err.Description, vbCritical
Resume ReadTextFileExit
End Sub