Friday 6 April 2012

Function Call to read a Text File

Function ReadTextFile(strFilePath)

' reading a file line by line

Const ForReading = 1

' create file system object
Set objFS = CreateObject("Scripting.FileSystemObject")

' first check that the file exists
If objFS.FileExists(strFilePath) Then

' open the text file for reading
Set objFile = objFS.OpenTextFile(strFilePath, ForReading, False)

' do until at end of file
Do Until objFile.AtEndOfStream

' store the value of the current line in the file
strLine = objFile.ReadLine

' show the line from the file
MsgBox strLine

Loop ' next line

' close the file
objFile.Close

Set objFile = Nothing

Else ' file doesn't exist

' report a failure
Reporter.ReportEvent micFail, "Read File", "File not found"

End if ' file exists

' destroy the objects
Set objFS = Nothing

End Function

No comments:

Post a Comment