VBA OpenTextFile

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)

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

Name of the text file to open.
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

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.
Defines the coding of the text file:

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

VBA OpenTextFile Examples

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

Referencing Microsoft Scripting Runtime

Not referencing Microsoft Scripting Runtime

Simply the best place to learn Excel VBA