Table of contents
Collapse the table of content
Expand the table of content

OpenAsTextStream Method

office 365 dev account|Last Updated: 6/12/2017
1 Contributor

Description Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file. Syntaxobject. OpenAsTextStream( [ iomode, [ format ]] ) The OpenAsTextStream method syntax has these parts:

objectRequired. Always the name of a File object.
iomodeOptional. Indicates input/output mode. Can be one of three constants: ForReading, ForWriting, or ForAppending.
formatOptional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

Settings The iomode argument can have any of the following settings:

ForReading1Open a file for reading only. You can't write to this file.
ForWriting2Open a file for writing. If a file with the same name exists, its previous contents are overwritten.
ForAppending8Open a file and write to the end of the file.

The format argument can have any of the following settings:

TristateUseDefault-2Opens the file using the system default.
TristateTrue-1Opens the file as Unicode.
TristateFalse0Opens the file as ASCII.

Remarks The OpenAsTextStream method provides the same functionality as the OpenTextFile method of the FileSystemObject. In addition, the OpenAsTextStream method can be used to write to a file. The following code illustrates the use of the OpenAsTextStream method:

Sub TextStreamTest
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
    Dim fs, f, ts, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.CreateTextFile "test1.txt"            'Create a file
    Set f = fs.GetFile("test1.txt")
    Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
    ts.Write "Hello World"
    Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
    s = ts.ReadLine
    MsgBox s
End Sub
© 2018 Microsoft