Text File Printing

This tip covers the pesky Visual Basic process that is text file printing.

Text File Printing
David Sharpe

New to VB programming? David Sharpe, one of our subscribers, was as well, when he ran into a problem. It's an easy problem, but sometimes the easiest problems cause the most trouble. Anyway, David sends this tip in hopes that his investigations will help others. If you have a better solution to this problem, why not share it with other VB developers. If we pick your tip, you'll get a free SearchVB denim shirt.

I am somewhat new to VB, but I have a good background in DOS Foxpro and some exposure to QuickBasic. A VB 6 program I'm writing was to create a document as a text file on the PC (to store for historical reasons) and then simply send it to the printer for a hard copy. I initially found it difficult to determine how to create the local text file, then really got lost when I attempted to find out how to get it printed.

I was a finally able to figure it out, and thought my findings might be helpful or informative in some way to others.

The basic creation of the local text file seems to follow older basic structures, although I could find no document in MSDN that helped. So here's how I did it.

The following would read all of the lines (txtLine) in the file "TxtFileToRead" and write each line into "TxtFileToWrite"

Dim txtLine as String
Dim TxtFileToRead  as string
Dim TxtFileToWrite as string

Open TxtFileToRead  for Input  as #1
Open TxtFileToWrite for Output as #2

Do While Not EOF(1)

Line Input #1 , txtLine
Print #2 , txtLine

Loop

Close #1
Close #2

To print a file requires use of two methods of the "Printer Object". The follwing will read each line (txtTheLine) in a text file (txtTheFile), store it in the Printer Object and then causes the Printer Object to be actually be sent to the printer.

dim txtTheLine as string        'declare the variable
    'that you intend to
    'store the lines that
    'are read in.

dim txtTheFile as string 'declare the variable
    'that you intend to use to
    'store the file's name.

txtTheFile = "c:program filesvb projectsmy_data.txt"

    'assign the file's fully
    'qualified path and name
    'to the variable.

Open txtTheFile for Input as #1 'Open the file.

Do While Not Eof(1)  'Prepare to read the file
    '(while not Eof(1) causes
    'termination when the End
    'Of File is reached.

Line Input #1, txtTheLine 'Read a line
Printer.Print txtTheLine 'Store the line into the
    'Printer Object

Loop    'Repeat the process of
    'reading lines

Printer.Enddoc   'Causes the lines stored
    'via the "Printer.Print"
    'line above to be sent to
    'the printer. It also sends
    'a Form Feed to cause
    'the printed page to be
    'printed properly.

I hope that this will be of benefit to others.

Thanks, David. For your interest in SearchVB, we'll be sending you a denim shirt.

"I've been wrestling with PCs and databases since 640k of ram was 'Dreamed for,' neither Dbase nor MSDos had revision levels, and GW-Basic was IT!"
Dave Sharpe works in the Materials Management Department of BellSouth Communication Systems in Roanoke,VA. His major functions involve ad hoc reporting from the corporate Oracle databases using MSAccess.


This was first published in December 2000

Dig deeper on Win Development Resources

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close