FileSystemObject:OpenTextFile Method

FileSystemObject:OpenTextFile Method

Description :

This function opens a file and returns a TextStream object and later you can use that TextStream object to perform various activities on that file like reading or writing

Format :

objectOfFileSystemObject. OpenTextFile(filename[, iomode[, create[, format]]])

Arguments: 
  • objectOfFileSystemObject : FileSystemObject
  • filename:
    • Mandatory
    • Type: String
    • File needs to be opened.

  • iomode
    • Optional
    • Constants:
      • ConstantValueDescription
        ForReading1Open a file for reading only. You can’t write to this file.
        ForWriting2Open a file for writing.
        ForAppending8Open a file and write to the end of the file.
  • Create
    • Optional
    • Type: Boolean ( True: creates a new file with the name provided if file doesn’t   exist,       False : file is not created if not exist)
  • format
    • Optional
    • Tristate values indicate the format of the opened file. If not given, it will be opened in ASCII
    • TristateUseDefault-2Opens the file using the system default.
      TristateTrue-1Opens the file as Unicode.
      TristateFalse 0Opens the file as ASCII



Open a text file:
Open a text file from specific location and returns a TextStreamObject

Method Name: OpenTextFile

Function FnOpenTextFile
Set fso = CreateObject("Scripting.FileSystemObject")

Set filetxt = fso. OpenTextFile ("c:\sample.txt",1, True)

 'So you can use this TextStreamObject for further operations on that text file like Writing something in it or closing it.

MyFile.WriteLine("Sample Text in text file")

MyFile.Close

End Function

 

Leave a Comment