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. VBA Excel – Refer to Multiple Ranges : Union Method
  2. VBA-Excel: Copy/Paste data - Copy the Entire row data and paste it to another row
  3. Getting Started with Excel Macro
  4. Introduction to Excel WorkBook
  5. VBA-Excel: Get all the WeekDays or Working days in Specified Date Range, (excluding Satudays and Sundays)
  6. FileSystemObject : GetDriveName Method
  7. VBA-Excel: SUDOKU Solver
  8. VBA Excel - Cells, Ranges and Offset : Range