View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Rob Hargreaves[_2_] Rob Hargreaves[_2_] is offline
external usenet poster
 
Posts: 32
Default VBA output to text editor

Hi Stuart,

In my experience I have always gone around collecting copies in word of
information on coding VBA I think might be useful.

You will find the piece below which deals with I/O with VBA.

Hope its useful

Rob

The first thing that you may want to do with a file is to open it. This
operation is performed with the Open command:

Open <filename For <mode As <filenumber

filename can be any valid path to the file. All of these are valid paths:

"myfile.dat"
"mydir\myfile.txt"
"c:\mydir\myfile.bin"
"..\myfile.bmp"

<mode specifies the method by which you want to work with this file. There
are 5 file modes:

-Input: This mode allows you to read text item by item from a text file.
This mode is useful exclusively for plain text (human-readable) files.
-Output: This mode allows you to write text item by item to a text file. The
resulting file is readable in the Input mode. The file is erased when opened
in this mode.
-Append: This mode is the same as Output, but the file is not erased.
Rather, writing starts at the end of the file. Useful for log files, among
other things.
-Binary: Binary access will read however many bytes is necessary to fill
the provided variable. It can read and write to any arbitrary byte. This is
useful for reading some data files, such as image files. You specify
your position in the file in Bytes.
-Random: Is similar to Binary, but only allows reading and writing to record
boundaries. The size of the record is specified in the open statement, or
defaults to 128 bytes if not specified. You specify your position in the
file by record number.

<filenumber is an identifying number for the file. This is passed to
subsequent statements to tell them which file to perform their operation on.
This also means that you can have many files open at once. When dealing with
a lot of files, it may not be easy to know what filenumbers are in use. To
help with this, we have the FreeFile() function. By storing the return of
this function, we get the lowest unused file number. Here is an example of
opening a file using a call to FreeFile():

sub FreeFile()
Dim FF As Integer
FF=FreeFile()
Open "myfile.txt" For Input As FF
End Sub

Reading & Writing

Text files are accessed with the Input and Print statements. In older
versions of BASIC, these statements included a # sign; this convention has
been dropped. The Input and Output statements have this syntax:

Input <filenumber,<variable
Print <filenumber,<expression

In both statements, <filenumber is the number of the open file. If there is
no open file with the number passed, an error will result.

Input reads from the file into the variable passed as <variable. Strings
are read separated by commas or grouped by quotes. If you read a numeric
data type (integer, single, etc) VB will start reading from the end of white
space (newlines, tabs, and spaces) until it finds a non-digit. Thus, if
there are characters instead of digits, 0 will be read. This also means that
more than 1 number can be on a line. User defined types and classes cannot
be read from text files, as there is no way for VB to figure out how they
are read.

Print writes the result of <expression on a new line in the file. An
expression is a literal ("word", 99, 1.2, etc), a variable, or a function
(2+2, string1 & string2, etc). The following are all valid expressions:

1+2*3
myvar+2
str$(myinteger) & mystring
"this is a literal"
1002

Sometimes we want to read a particular number of characters from a file into
a string, regardless of how they are broken among lines. This is useful for
reading an entire file at once, among other things. The Input function is
used to do this:

Input(<number,<filenumber)

<number specifies how may characters to get, and <filenumber is the index
that you assigned to the file when you opened it. This is a function, so you
assign its return to a variable.

Another statement for writing to text files is Write:

This statement works in the same way as Print, except that you can pass
multiple things as <output, and certain formatting changes are made when
the data is written to the file. These changes can be summarized as follows:

-Individual pieces of data are separated by commas
-Strings are enclosed in quotation marks
-Dates and booleans are converted to a format readable in any locale (other
languages and data formats)

These are some example Write statements, and what they write to a file:

Write FF,99,100,75,"foo","bar",23.4
'99,100,75,"foo","bar",23.4
Write FF,True,MyString,45.3
'#TRUE#,"contents of MyString",45.3
Write FF,True,12.4,False
'#TRUE#,12.4,#FALSE#

So, the principle difference between Print and Write, is that Print is best
suited for creating a report, or some other document that someone would read
with an editor or print out. Write was made for saving data and reading it
back into the computer, not producing a document. Of
course, you can use print to save data and read it back, but depending
on the data, can be more complicated. If you had 3 strings that made up an
entry in an address book for instance, it is easier to put those three
strings on a single line in your file, using Write:

Write FF, String1, String2, String3

In that example, if one of your strings had a comma in it, then the line
written with Print would not read back in correctly since Input would see
more than 3 strings in the line. The line written using Write would read
back correctly because the strings will have double quotes around it. And if
perchance one of the Strings had a double quote in the string, the Write
will still work because it will put "" (two double quotes) in the string
when written to the file (two double quotes together is how you designate a
quote character within a string).

It should be mentioned that when using Print, just like it does when
printing to a form, or printing to a picturebox, a comma will space the
printed data into columns and a semicolon will suppress the CRLf, so that
further prints are on the same line.

Closing the File

While VB will close all files when your program ends, it is a good idea to
close a file when you are done with it (you can always open it again later).
One reason for this is that file numbers are global. You can pass a file
number as an argument to other modules and classes, and they can operate on
that file as though that module had opened it. Another reason is that should
your program close in an uncontrolled manner (sudden process death, hard
reboot, etc) damage may occur to the file. The Close statement works as
follows:

Close <filenumber

If <filenumber isn't the number of an open file, no error will be thrown.
Thus, there is never a danger of getting an error while closing a file.
If you don't specify a filenumber, then all open files are closed. You can
also specify multiple filenumbers to close multiple files, but not
necessarily all.