Opens the specified file and returns a TextStream object that can be read, written, or appended to the file.
object.OpenTextFile(filename[, iomode[, create[, format]]])
parameter
object: required. Expected to be the name of a FileSystemObject object.
filename: required. A string expression specifying the name of the file to open.
iomode: optional. Input/output mode, one of the following three constants: ForReading, ForWriting, or ForAppending.
create: optional. Boolean value indicating whether a new file can be created when the specified filename does not exist. True to allow creation of new files, False otherwise. The default value is False.
format: optional. One of three Tristate values indicating the format in which to open the file. If this parameter is omitted, the file is opened in ASCII format.
set up
The iomode parameter can be one of the following settings:
Constant value description
ForReading 1 opens the file in read-only mode. This file cannot be written.
ForWriting 2 opens the file for writing only. This file cannot be read.
ForAppending 8 opens the file and writes to the end of the file.
The format parameter can be one of the following settings:
Constant value description
TristateUseDefault -2 opens the file in the system default format.
TristateTrue -1 opens the file in Unicode format.
TristateFalse 0 Open the file in ASCII format.
illustrate
The following code illustrates how to use the OpenTextFile method to open and write a file:
Copy the code code as follows:
Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject(Scripting.FileSystemObject)
Set f = fso.OpenTextFile(c:/testfile.txt, ForWriting, True)
f.Write Hi there!
f.Close
End Sub