VBA open text file using File System Object

Home/VBA/VBA open text file using File System Object

VBA code to open text file will help you to read the text file data. VBA open text file example will show you how to open a text file using FileSystem Object. We can open the Text File for Reading the data, Appending the data and writing the data.



50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates

VBA code to open text file using FileSystem Object

Following is the VBA syntax and VBA example to open a file using File system object in VBA.

VBA code to open text file using FileSystem Object: Syntax

Here is the VBA code and syntax to open the text file using VBA Fiel System Object. Here we need to pass the text file path to open using VBA.


VBA open text file using FileSystem Object: Syntax

Following is the example VBA code to open the text file suing VBA. Here we need to add reference to ‘Microsoft Scripting Runtime library‘ to use the FSO library in our code. First we are creting the FileSystem Object and opening the text file. Then we are reading the each line in the text file until end of the File.

Sub sb_VBA_To_Open_TextFile_FSO()
Dim strFile As String
Dim myFSO As New FileSystemObject
strFile = "C:\temp\test.txt"
Set fso = myFSO.OpenTextFile(strFile)
Do Until fso.AtEndOfStream
MsgBox fso.ReadLine
End Sub

VBA code to open text file using FileSystem Object: Explained Code

‘Starting procedure to write VBA code to open text file using File system object
Sub sb_VBA_To_Open_TextFile_FSO_C()

‘Delcaring the variables

‘strFile is declared as string to capture file name
Dim strFile As String

‘myFSO is declared as new FileSystemObject
Dim myFSO As New FileSystemObject

‘Assigning the file path to strFile variable
strFile = “C:\temp\test.txt”

‘opening the file with File system object and setting fso object
Set fso = myFSO.OpenTextFile(“C:\temp\test.txt”)

‘Reading the data until end of the file by each line using do loop statement
Do Until fso.AtEndOfStream
‘displaying the each line
MsgBox fso.ReadLine

‘Ending the do loop

‘Ending the sub procedure
End Sub

By |April 22nd, 2014|VBA|2 Comments

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).


  1. Balaji August 15, 2014 at 11:52 PM - Reply

    Hello Sir,
    I was trying to use the code provided by you, to open the outlook signature text(.txt) file from the signature folder. I am passing the arguments, path and readmode to OpenTextFile but when I try to extract the contents into string it is not taking the values in the file but is showing me some junk.
    When I copy the Msgbox output it looks like below
    Microsoft Office Outlook
    I dont know where I am going wrong.

    • PNRao August 17, 2014 at 11:41 AM - Reply

      Hi Balaji,

      Please try to open the text file using ADO, it should solve your issue.


Leave A Comment

Related pages

how to find out the duplicate values in excelhow to unhide rows in excel 2013compile data from multiple excel worksheetsdifferent types of languages in sqlexcel vba tips and tricksgetopenfilename vbaexcel auto open macrounprotect sheet vbahow to filter out duplicates in excelhow to unprotect excel with passwordunlock excel spreadsheet without passwordvba import excel into access tableexcel 2010 remove blank rowsvba select rowschecklist excel templateunprotect excel spreadsheetvba loop syntaxnotepad vbaexcel 2007 create drop down listnested if function excel 2010 examplesumifs examplesapplication object vbausing the countif function in excelhow to loop in vbafiledialog filterpie chart template excelcalculate duration between two dates excelvba insert rowshow do you merge columns in excelcool excel formulascolor vbaadvanced excel exercisessql where operatorsvba nested if statementsms excel 2007 shortcut keys listvbscript read excel filesql aptitude questions and answersdata definition language commandsunhide hidden columns in excelado connection in vbword vba formshortcut keys of excelvba torrenthandy excel tipscontoh minutes of meetingexcel graph wizardexcel combine data from multiple worksheetsvba worksheet cellsexcel 2013 create macrodml commands in sql serverexcel vba programming pdfexcel vba collectionsvba countexcel vba messageboxsumif formula examplesql interview questions and answers for freshers pdf downloadpassword protect xlsunlock excel sheetexcel vba if cell containsexcel unhide rowstask excel templatewhat is the meaning of vlookuphow do you add developer tab to ribbonexcel vba select columnpivot table excel vbaarrange numbers in ascending order worksheetsprotect cells in excel 2007excel filter table based on cell valueremove hyperlink excelvba excel delete rowcode in vbapaste in vbaoffset formula in excelvisual basic comboboxhow do you create a macro in excelhow to build a pivot table in excel 2007