Be the first user to complete this post

  • 0
Add to List

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:
      • Constant Value Description
        ForReading 1 Open a file for reading only. You can't write to this file.
        ForWriting 2 Open a file for writing.
        ForAppending 8 Open 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 -2 Opens the file using the system default.
      TristateTrue -1 Opens the file as Unicode.
      TristateFalse  0 Opens 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



Also Read:

  1. WorkBooks and WorkSheets
  2. VBA Excel - Cells, Ranges and Offset: Refer to Cells by Using Shortcut Notation
  3. FileSystemObject : DeleteFolder Method
  4. FileSystemObject : FileExists Method
  5. VBA-Excel: Add/Insert multiple objects from a folder in an Excel Document.