OpenTextFile Method

Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.

Syntax

object.OpenTextFile(filename[, iomode[, create]])

Arguments:

  • Object: Required. Object is always the name of a FileSystemObject.
  • Filename: Required. String expression that identifies the file to open.
  • Iomode: Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending.
  • Create: Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value is True if a new file is created, False if it isn't created. If omitted, a new file isn't created.
  • Format: Optional. Indicate the format of the opened file.

    Note:

    Format argument is dormant and kept to mimic VB script syntax. It has no effect. All the files created are in Unicode format only.

Settings

The iomode argument can have any of the following settings:

Table 11-34 IOMode Argument Settings

Constant Value Description
ForReading 1 Open a file for reading only. You can't write to this file.
ForWriting 2 Open a file for writing.
ForWriting 8 Open a file and write to the end of the file.

Remarks

The following code illustrates the use of the OpenTextFile method.

Note:

  • If a New File is created, then it will be created with Unicode format.

  • If File does not Exists code will throw an error in ReadMode.

Example 1:

Sub OpenFileForReading()
    Const ForReading = 1
    Dim fso, tso
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set tso = fso.OpenTextFile("C:\example1.txt", ForReading)    
    tso.Close               ' Close the file.

    Set tso = Nothing
    Set fso = Nothing
End Sub

Call OpenFileForReading
'If C:\example1.txt is not present then code will throw an error. 

Example 2:

Sub OpenFileForWriting()
    Const ForWriting = 2
    Dim fso, file
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile("C:\example2.txt", ForWriting, True)
    
    file.WriteLine("This is written to the file.")   ' Writes text to the file.
    file.Close               ' Close the file.

    Set file = Nothing
    Set fso = Nothing
End Sub

Call OpenFileForWriting
'Creates C:\example2.txt and writes a line "This is written to the file."

Example 3:

Sub OpenFileForAppending()
    Const ForAppending = 8
    Dim fso, file
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile("C:\example2.txt", ForAppending, True)
    
    file.WriteLine("This line is appended.")   ' Appends text to the file.
    file.Close               ' Close the file.

    Set file = Nothing
    Set fso = Nothing
End Sub

Call OpenFileForAppending
'Creates C:\example2.txt and writes a line "This line is appended.". If file already exists it appends this line.

Example 4:

Sub OpenFileWithCreateOption()
    Const ForWriting = 2
    Dim fso, file
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile("C:\example2.txt", ForWriting, True)   ' Creates the file if it doesn't exist.

    file.WriteLine("New File created and text written.")   ' Writes text to the file.
    file.Close               ' Close the file.
 
    Set file = Nothing
    Set fso = Nothing
End Sub

Call OpenFileWithCreateOption
'Overwrites C:\example2.txt and writes a line "New File created and text written.". If file does not exists then it creates it.