DIAdem Help

Method: OpenAsTextStream for File

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

Method: OpenAsTextStream for File

Opens a file for reading, writing, or appending and returns a TextStream object.

Set oTextStream = Object.OpenAsTextStream([IOMode], [Format])
ObjectFile
Object with this method
[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.
[Format]Enumeration with the following selection terms:
-2TristateUseDefaultOpens the file in the format of the system standard.
-1TristateTrueOpens the file in the Unicode format.
0TristateFalseOpens the file in the ASCII format. This is a default value.
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, 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

Log in to get a better experience