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
objectOfFileSystemObject. OpenTextFile(filename[, iomode[, create[, format]]])
- objectOfFileSystemObject : FileSystemObject
- Type: String
- File needs to be opened.
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.
- Type: Boolean ( True: creates a new file with the name provided if file doesn’t exist, False : file is not created if not exist)
- 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