Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Getting Input from a Text File.

Getting Input from a Text File

by Allen Wyatt
(last updated May 6, 2017)


True to its BASIC roots, VBA allows you fetch input (information) from sequential files. This means you can open and read a sequential text file, loading the information from the file into string variables. The steps are simple. You only have to open the file, get the input, and then close the file. The following code is a common example of reading from a sequential file:

Dim Raw As String
Dim NumValues As Integer, J As Integer
Dim UserVals() As String

Open "MyFile.Dat" For Input As #1
Line Input #1, Raw
NumValues = Val(Raw)
ReDim UserVals(NumValues)

For J = 1 to NumValues
    Line Input #1, UserVals(J)
Next J
Close #1

You should note that the first line read from the text file (MyFile.Dat) is assumed to contain a value that indicates how many items are to be read in from the file. The Open statement is used to open the text file (MyFile.Dat) and assign it a file number, in this case the number 1. This file number is then subsequently used by various statements (such as Line Input and Close) to reference the file.


If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (10477) applies to Microsoft Word 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Word here: Getting Input from a Text File.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...


Making Pictures Show in Word

What are you to do if you can't see all the pictures you know are in your document? The answer may lie in where those ...

Discover More

Printing Just the Visible Data

In a large worksheet, you may want to display and print just a portion of the available data. Displaying the desired ...

Discover More

Turning Off Document Protection

If you protect your document using the tools that Word provides, at some time you may need to turn off that protection. ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Combining Word Documents

At some point you may want to insert one Word document inside another Word document. An easy way to do this is to use the ...

Discover More

Use Filenames that Sort Properly

If you take some care when you name your document files, you'll find it much easier to manage those files at a later ...

Discover More

Setting Your Default Document Directory

Word allows you to specify where it should start looking for your documents. This setting can come in handy if you store ...

Discover More

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 1?

2017-05-09 04:48:08

Alan Elston

I think EOF Function is often used in similar codes.
I expect Allen is looking in particular at a way to do this putting the data into an Array without having to RE Dim Preserver the Array each time in the Loop to adjust it size. ( Normally you would not have, as far as I know, a simple way of determining the number or records ( rows ) in a text file).
So Allen suggest you put this number as the first row in your text File. You then get that number first . You get that number as a string. But VBA is kind with strings that look like numbers and will usually accept it as a number as it probably would in this case.
Actually, Allen uses the val function so allows the number to be returned from like a first row in the text file of
"10 is the Number of rows I have here" – this would return 10
That number he then uses in the sizing of his Array.

Another alternative would be to use the EOF Function way in conjunction with adding the retrieved rows each time in the loop to a dictionary or a ListArray or collection object , etc. Then you can often get the final Array out from the collection object using some Method or Property
In the practice you would probably need to test speeds etc., too se if there are any advantages over just re sizing an Array in the Loop

This is just one way to use the EOF Function and get row information from a text file into an Array without having to re size the array each time in the Loop:

Sub ListArrayEOFWay() '
Dim Raw As String
Dim UserVals() As Variant
Open "MyFile.txt" For Input As #1
With CreateObject("System.Collections.ArrayList")
Do While EOF(1) = False
Line Input #1, Raw
.Add Raw
Let UserVals() = .ToArray() ' The Method .ToArray writes all elements of the ArrayList into a 1-dimensional Array, '
End With
Close #1
End Sub


2017-05-08 04:34:38


Would it be possible to use the EOF function? This would remove the need to specify how many items to read from the file.

This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.