O'Reilly logo

Excel® 2007 VBA Programmer's Reference by Michael Alexander, Rob Bovey, Stephen Bullen, John Green

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 11. Text Files and File Dialog

Text files provide one way to communicate information between different types of computers. There is no universal format for the binary files that usually provide the most efficient format for working within a particular computer system, so text files are often used for this. This chapter examines how to create text files and how to read them.

Increasingly, XML is becoming the standard way to exchange data across the Internet. XML files are text files, but in a highly organized format. They are covered in Chapter 12. However, there are many legacy systems, particularly where mainframe computers are concerned, where text files in a variety of formats are used.

Excel is capable of importing text files and can save data in .csv (comma separated variables) and .prn (print) files, as well as other formats. Often these features are not flexible enough to cater to specific needs. Using VBA, you can produce text files in whatever format you like and read text files in whatever format is provided.

This chapter also discusses the FileDialog object, which allows you to display the Office dialogs for opening and saving files and browsing folders.

Opening Text Files

Before you can read or write to a file, you need to open it using the Open statement. When opening a text file for sequential access, Open has the following options:

Open file [For mode] As [#]filenumber

The mode can be Input, Output, or Append. If you specify Output and there is an existing file with ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required