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

Get Excel® 2007 VBA Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.