Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andy Brown
In this tutorial
This tutorial shows how to read from (and write to) text files using something called TextStream objects.
There is another way to do this in VBA using commands like Input and Open, but I think the way shown here is much easier to remember and use.
In order to get any of the code in this tutorial to work, you first need to create a FileSystemObject.
The code on this page will create a new text file on your hard disk:
The code will create the file called info.txt on your hard disk.
Here's what the contents of the text file will look like:
The code will write out the two lines!
The first thing we need to do is to create a reference to the Microsoft Scripting Runtime object library, and then create a FileSystemObject - refer to my separate FileSystemObject tutorial for how to do this and what it means:
Sub CreateFile()
'we need this, even though it doesn't make much sense!
Dim fso As New FileSystemObject
To do this, apply the CreateTextFile method to the weird FileSystemObject:
'the file we're going to write to
Dim ts As TextStream
'open this file to write to it
Set ts = fso.CreateTextFile("C:\Wise Owl\info.txt", True)
In this case, the second argument True refers to whether we'll overwrite any existing version of the file when we create the new one. We choose to do so.
You can use the WriteLine method to write lines of text, but you must then close the file:
'write out a couple of lines
ts.WriteLine ("If anyone finds this file")
ts.WriteLine ("they will wonder who created it")
'close down the file
ts.Close
End Sub
Here's the entire routine for ease of copying and reference:
Sub CreateFile()
'we need this, even though it doesn't make much sense!
Dim fso As New FileSystemObject
'the file we're going to write to
Dim ts As TextStream
'open this file to write to it
Set ts = fso.CreateTextFile("C:\Wise Owl\info.txt", True)
'write out a couple of lines
ts.WriteLine ("If anyone finds this file")
ts.WriteLine ("they will wonder who created it")
'close down the file
ts.Close
End Sub
Now that we've successfully created a text file, how can we read it in? To do this, we need to open another TextStream object.
Having created a text file, here's some code to read it in. The trick is to keep reading lines until we reach the end of the text stream:
Sub ReadFile()
'again, we need this strange thing to exist so that ...
Dim fso As New FileSystemObject
'the file we're going to read from
Dim ts As TextStream
'... we can open a text file with reference to it
Set ts = fso.OpenTextFile("C:\Wise Owl\info.txt", ForReading)
'keep reading in lines till no more
Dim ThisLine As String
Dim i As Integer
i = 0
Do Until ts.AtEndOfStream
ThisLine = ts.ReadLine
i = i + 1
Debug.Print "Line " & i, ThisLine
Loop
'close down the file
ts.Close
End Sub
Here's what this would show in the immediate window:
The routine prints out the lines to the immediate window
When you choose to open a text file, you can do it in one of 3 ways:
The 3 possible ways to open a text stream
In our example we wanted to read from the file, but you can also use the OpenTextFile method to open a file to write more lines to it (using ForAppending).
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.