OpenAsTextStream Method
Open the specified file and returnTextStreamObject. This object is used to read, write, or append an object.
object.OpenAsTextStream([iomode, [format]])
Parameters
Object
Required. Should beFileObject Name.
Iomode
Optional. The input/output mode is one of the following three constants: ForReading, ForWriting, or ForAppending.
Format
Optional. ThreeTristateIndicates the format in which the file is opened. If this parameter is ignored, the file is opened in ASCII format.
Set
IomodeThe parameter can be one of the following settings:
| Constant |
Value |
Description |
| ForReading |
1 |
Open the file in read-only mode. You cannot write this file. |
| ForWriting |
2 |
Open a file in read/write mode. If a file with the same name already exists, the old file will be overwritten. |
| ForAppending |
8 |
Open the file and write it at the end of the file. |
FormatThe parameter can be one of the following settings:
| Constant |
Value |
Description |
| TristateUseDefault |
-2 |
Open a file in the default format. |
| TristateTrue |
-1 |
Open a file in Unicode format. |
| TristateFalse |
0 |
Open an object in ASCII format. |
Description
The OpenAsTextStream method provides the same functions as the OpenTextFile method of the FileSystemObject object. In addition, you can use the OpenAsTextStream method to write files.
The following code demonstrates how to useOpenAsTextStreamMethod:
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") Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault) ts.Write "Hi!" ts.Close Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault) TextStreamTest = ts.ReadLine ts.CloseEnd Function