The FileSystemObject VBA OpenTextFile function opens a file as a TextStream object for text read, write, or append (does not allow binary operations).
VBA FileSystemObject Methods
- BuildPath
- CopyFile
- CopyFolder
- CreateFolder
- CreateTextFile
- DeleteFile
- DeleteFolder
- DriveExists
- FileExists
- FolderExists
- GetAbsolutePathName
- GetBaseName
- GetDrive
- GetDriveName
- GetExtensionName
- GetFile
- GetFileName
- GetFolder
- GetParentFolderName
- GetSpecialFolder
- GetTempName
- MoveFile
- MoveFolder
- OpenTextFile
VBA OpenTextFile Syntax
1 |
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 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
'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 |
Not referencing Microsoft Scripting Runtime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
'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 |