Method: OpenAsTextStream for File
- Updated2024-09-12
- 2 minute(s) read
FileSystemObject and Dictionary > Methods > Method: OpenAsTextStream for File
Method: OpenAsTextStream for File
Opens a file for reading, writing, or appending and returns a TextStream object.
Set oTextStream = Object.OpenAsTextStream([IOMode], [Format])
| Object | File Object with this method | |||||||||
| [IOMode] | Enumeration with the following selection terms:
| |||||||||
| [Format] | Enumeration with the following selection terms:
| |||||||||
| oTextStream | TextStream Returned object |
The following example opens a text file where it writes a text line. Then the example reopens the file to append a text. The example then reopens the file to read the text:
Sub ReadWriteAppend(sFile) Const ForReading = 1, ForWriting = 2, ForAppending = 8 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim fso, oMyFile, oMyFileObj Set fso = CreateObject("Scripting.FileSystemObject") 'Get file object Set oMyFileObj = fso.GetFile(sFile) ' Open the file for output Set oMyFile = oMyFileObj.OpenAsTextStream(ForWriting) ' Write to the file. oMyFile.WriteLine "This is the first line" oMyFile.Close ' Open the file for appending Set oMyFile = oMyFileObj.OpenAsTextStream(ForAppending) ' Write to the file. oMyFile.WriteLine "This is the second line" oMyFile.Close ' Open the file for input Set oMyFile = oMyFileObj.OpenAsTextStream(ForReading) ' Read from the file Call MsgBox(oMyFile.ReadAll) oMyFile.Close End Sub
See Also
Related Topics
Command: ExecuteExclusiveBegin | Command: ExecuteExclusiveEnd | Command: ExecuteExclusiveEndAll