OpenTextFile method
Opens the specified file and returns a TextStream object that can be read, written to, or appended to the file.
object.OpenTextFile(filename[, iomode[, create[, format]]])
Parameters
Object
Required option. Should be the name of the FileSystemObject object.
FileName
Required option. A string expression that indicates the name of the file to open.
IOMode
Options available. The input/output mode is one of the following three constants: Forreading,forwriting, or ForAppending.
Create
Options available. Boolean value that indicates whether a new file can be created when the specified filename does not exist. TrueWhen new files are allowed to be created, otherwise False. The default value is False.
Format
Options available. One of three tristate values that indicates in what format the file is opened. If this argument is omitted, the file is opened in ASCII format.
Set up
The iomode parameter can be one of the following settings:
Constants |
value |
Description |
ForReading |
1 |
Open the file in read-only mode. This file cannot be written to. |
ForWriting |
2 |
Opens the file as a write-only method. This file cannot be read. |
ForAppending |
8 |
Open the file and write at the end of the file. |
The 〈p〉 format parameter can be one of the following settings:
Constants |
value |
Description |
Tristateusedefault |
-2 |
Open the file in the system default format. |
TristateTrue |
-1 |
Opens the file in Unicode format. |
Tristatefalse |
0 |
Opens the file in ASCII format. |
Description
The following code illustrates how to open a write file using the OpenTextFile method:
Sub opentextfiletest Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso, f Set fso = Createobjec T ("Scripting.FileSystemObject") Set f = FSO. OpenTextFile ( "C:\testfile.txt" , for Writing, True ) F.write "
" Hi, Hello!
f.close