OpenAsTextStream method
Opens the specified file and returns a TextStream object that is used to read, write, or append to the file.
object.OpenAsTextStream([iomode, [format]])
Parameters
Object
Required option. Should be the name of the File object.
IOMode
Options available. The input/output mode is one of the following three constants: ForReading, ForWriting, or forappending.
Format
Options available. One of three tristate values that indicates in what format the file is opened. Ignoring this parameter, 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 |
Open the file in read-write mode. If a file with the same name already exists, the old file is overwritten. |
| ForAppending |
8 |
Open the file and write at the end of the file. |
The 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 OpenAsTextStream method can provide the same functionality as the OpenTextFile method of a FileSystemObject object. In addition, you can write files by using the OpenAsTextStream method.
The following code illustrates how to use the OpenAsTextStream method:
Function TextStreamTest Const ForReading = 1, ForWriting = 2, ForAppending = 8 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim fso, f, ts Set fso = CreateObject("Scripting.FileSystemObject") fso.CreateTextFile "test1.txt" 'Create a file. Set f = fso.GetFile("test1.txt") f.OpenAsTextStream(ForWriting, TristateUseDefault) ts.Write " Hi, Hello!" ts.Close f.OpenAsTextStream(ForReading, TristateUseDefault) TextStreamTest = ts.ReadLine ts.CloseEnd Function