Method: OpenTextFile for FileSystemObject
- Updated2024-09-12
- 2 minute(s) read
FileSystemObject and Dictionary > Methods > Method: OpenTextFile for FileSystemObject
Method: OpenTextFile for FileSystemObject
Creates and opens a file for reading, writing, or appending and returns a TextStream object.
Set oTextStream = Object.OpenTextFile(FileName, [IOMode], [Create], [Format])
| Object | FileSystemObject Object with this method | |||||||||
| FileName | String String expression that indicates which file to open. | |||||||||
| [IOMode] | Enumeration with the following selection terms:
| |||||||||
| [Create] | Boolean Specifies whether DIAdem creates the file. | |||||||||
| [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 Set fso = CreateObject("Scripting.FileSystemObject") ' Open the file for output Set oMyFile = fso.OpenTextFile(sFile, ForWriting, True) ' Write to the file. oMyFile.WriteLine "This is the first line" oMyFile.Close ' Open the file for appending Set oMyFile = fso.OpenTextFile(sFile, ForAppending, True) ' Write to the file. oMyFile.WriteLine "This is the second line" oMyFile.Close ' Open the file for input Set oMyFile = fso.OpenTextFile(sFile, ForReading) ' Read from the file Call MsgBox(oMyFile.ReadAll) oMyFile.Close End Sub
See Also
Related Topics
Command: ExecuteExclusiveBegin | Command: ExecuteExclusiveEnd | Command: ExecuteExclusiveEndAll