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
Business Presentations Templates Pack
20+ Excel Project Management Pack
20+ PowerPoint Project Management Pack
10+ MS Word Project Management Pack
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 Loop 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
‘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
‘Ending the do loop
‘Ending the sub procedure