Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

FileSystemObject:OpenTextFile Method

FileSystemObject:OpenTextFile Method

Description :

This func­tion opens a file and returns a TextStream object and later you can use that TextStream object to per­form var­i­ous activ­i­ties on that file like read­ing or writing

Format :

objectOf­FileSys­te­mOb­ject. OpenTextFile(filename[, iomode[, cre­ate[, format]]])

Arguments: 
  • objectOf­FileSys­te­mOb­ject : FileSystemObject
  • file­name:
    • Manda­tory
    • Type: String
    • File needs to be opened.

  • iomode
    • Optional
    • Con­stants:
      • Con­stant Value Descrip­tion
        For­Read­ing 1 Open a file for read­ing only. You can’t write to this file.
        For­Writ­ing 2 Open a file for writing.
        ForAp­pend­ing 8 Open a file and write to the end of the file.
  • Cre­ate
    • Optional
    • Type: Boolean ( True: cre­ates a new file with the name pro­vided if file doesn’t   exist,       False : file is not cre­ated if not exist)
  • for­mat
    • Optional
    • Tris­tate val­ues indi­cate the for­mat of the opened file. If not given, it will be opened in ASCII
    • Tris­ta­teUseDe­fault –2 Opens the file using the sys­tem default.
      Tris­tateTrue –1 Opens the file as Unicode.
      Tris­tate­False  0 Opens the file as ASCII



Open a text file:
Open a text file from spe­cific loca­tion 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

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: