VBA OpenTextFile

The FileSystemObject VBA OpenTextFile function opens a file as a TextStream object for text read, write, or append (does not allow binary operations).

VBA OpenTextFile Syntax

fso.OpenTextFile( filename, [ iomode, [ create, [ format ]]] )

filename
Name of the text file to open.
iomode
Optional. One of three options:

Option Value Description
ForReading 1 Open the file as read only
ForWriting 2 Open file for write only
ForAppending 8 Open for appending only. Does not allow to overwrite existing text only writing to the end of the file

create
Optional. If True will allow to create a new file if no such file exists. If False file will not be created and an error will be raised.
format
Defines the coding of the text file:

Option Value Description
TristateUseDefault -2 Default system coding
TristateTrue -1 Unicode coding
TristateFalse 0 ASCII coding

Proceed to next section to see some examples of this in action.

VBA OpenTextFile Example

Below are examples of using the OpenTextFile method. First we will explore writing and then reading a file.

'Below assumes you are referencing the Microsoft Scripting Runtime library
Dim fso As FileSystemObject, ts As TextStream
Set fso = New FileSystemObject

'The below will create Hello.txt if it does not exist and will open file for ASCII writing
Set ts = fso.OpenTextFile("C:\Hello.txt", ForWriting, True, TristateFalse)
ts.WriteLine "Hello"
ts.Close

'Open same file for reading
Set ts = fso.OpenTextFile("C:\Hello.txt", ForReading, True, TristateFalse)

'Read till the end
Do Until ts.AtEndOfStream
     Debug.Print "Printing line " & ts.Line 
     Debug.Print ts.ReadLine 'Print a line from the file
Loop
ts.Close

The above snippet references Microsoft Scripting Runtime. However, to avoid errors (library not found) I would avoid this. See next section to my recommended approach when using the FSO object.

Example using CreateObject

Below examples does not reference Microsoft Scripting Runtime – we use the CreateObject function instead. The drawback is that we will not be able to see the properties of the object. What I suggest is starting with the approach above and then when the code works – replacing New FileSystemObject with CreateObject(“Scripting.FileSystemObject”) as shown below.

'Below assumes you are NOT referencing the Microsoft Scripting Runtime library
Dim fso As Object, ts As Object
Const ForReading = 1, ForWriting = 2, ForAppending = 8 'Need to define constants manually
Const TristateUseDefault  = -2, TristateTrue = -1, TristateFalse = 0'Need to define constants manually
Set fso = CreateObject("Scripting.FileSystemObject")

'The below will not Hello.txt if it does not exist and will open file for Unicode appending
Set ts = fso.OpenTextFile("C:\Hello.txt", ForAppending, True, TristateFalse)
ts.WriteLine "Hello"
ts.Close

'Open same file for reading
Set ts = fso.OpenTextFile("C:\Hello.txt", ForReading, True, TristateFalse)

'Read till the end
Do Until ts.AtEndOfStream
     Debug.Print "Printing line " & ts.Line 
     Debug.Print ts.ReadLine 'Print a line from the file
Loop
ts.Close