bottom
Great WordTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > WordTips Home > Files > Getting Input from a Text File

Getting Input from a Text File

Summary: VBA includes some commands that you can use to read information from text files (non-Word documents). These commands can come in handy when you need to access a wide variety of information. This tip presents a very simple technique to use these commands and read information from a text file. (This tip works with Microsoft Word 97, Word 2000, Word 2002, Word 2003, and Word 2007.)

True to its BASIC roots, VBA allows you to do file input on 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.

Tip #1794 applies to Microsoft Word versions: 97 | 2000 | 2002 | 2003 | 2007


Great Idea! Word is a tool to get what you really want—printed output. This means you need to make sure that Word works as well as possible with your printer, whether it is sitting on your desk or in a room down the hall.
 
Check out WordTips: Printing and Printers today!

Helpful Links

Ask a Word Question
Make a Comment

Tips.Net Home
Tips.Net Store

WordTips FAQ
WordTips Premium

Learn Access Now

Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Word2007 Tips
WordTips

Advertise on the
WordTips Site

 

Great Info!

Get tips like this every week in WordTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)