DIAdem Help

Method: OpenTextFile for FileSystemObject

  • Updated2024-09-12
  • 2 minute(s) read

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])
ObjectFileSystemObject
Object with this method
FileNameString
String expression that indicates which file to open.
[IOMode]Enumeration with the following selection terms:
1ForReadingOpens the file for reading. This is a default value.
2ForWritingOpens the file for writing.
8ForAppendingOpens the file for writing. DIAdem writes new text at the end of the file.
[Create]Boolean
Specifies whether DIAdem creates the file.
[Format]Enumeration with the following selection terms:
-2TristateUseDefaultOpens the file in the format of the system standard. This is a default value.
-1TristateTrueOpens the file in the Unicode format.
0TristateFalseOpens the file in the ASCII format.
oTextStreamTextStream
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

Log in to get a better experience