REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

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.

VBA Open Text File

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.

myFSO.OpenTextFile("C:temptest.txt")

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:temptest.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
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:temptest.txt”

‘opening the file with File system object and setting fso object
Set fso = myFSO.OpenTextFile(“C:temptest.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
Loop

‘Ending the sub procedure
End Sub

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBATags: Last Updated: June 17, 2022

2 Comments

  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
    —————————
    ÿþT
    —————————
    OK
    —————————
    I dont know where I am going wrong.

  2. 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.

    Thanks-PNRao!

Leave A Comment