Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Starting to Print

The physical printing doesn't begin until all output is released to the print spooler, or until your application issues the EndDoc method.

As you send Print methods to the print spooler via the Printer object, the print spooler builds the page or pages of output but doesn't release that output until you issue an EndDoc method. EndDoc tells the print spooler, "I'm done sending output to you; you can print now."

Without EndDoc, Windows would collect all the application's output and not print any of the output until the application terminates. If you were to write an application that the user runs throughout the day and that prints invoices as customers make purchases, you would need to issue an EndDoc method at the end of each invoice-printing procedure if you wanted each invoice to print at that time.

Listing 16.3 prints a message on the printer and then signals to the print spooler that output is ready to go to paper. Without EndDoc, the print spooler would hold the output until the application containing the code terminated.

Example 16.3. Using EndDoc to release printed output.

1: Printer.Print "Invoice #"; invnum
2: Printer.Print "Customer:"; cust(CCnt); Tab(20); "Final Sales"
3: Printer.Print "Amount of sale:"; Tab(20); Format(SaleAmt, "Currency")
4: Printer.Print "Tax:"; Tab(20); Format(tax, "Currency")
5: Printer.Print
6: Printer.Print "Total:"; Tab(20), Format(TotalSale, "Currency")
7:
8:  ' Release the job for actual printing
9: Printer.EndDoc

The program containing Listing 16.3's code might continue to run and process other sets of data. The EndDoc method ensures that the output built in the preceding Print methods is sent to the physical printer immediately. If other Print methods appear later in the program, the print spooler begins building the output all over again, releasing that subsequent output only for an EndDoc procedure or when the application ends.

Page Breaks

The NewPage method forces the printer to eject the current page and begin subsequent output on the next new page.

The Windows print spooler ensures that each printed page properly breaks at the end of a physical page. Therefore, if the printer's page length is 66 lines and you print 67 lines, the 67th line will appear at the top of the second page of output. There are times, however, when you need to print less than a full page on the printer. You can release that incomplete page for printing using the NewPage method (shown previously in Table 16.2). To use NewPage, simply apply the Newpage method to the Printer object like this:

Printer.NewPage

Don't forget that you're working with printers that support many fonts and font sizes. You can always determine, in advance, how many lines of output will fit on a single page as long as you first check the value of the following formula:

intNumLinesPerPage = Printer.Height / Printer.TextHeight("X")

As explained in Table 16.3, the Height property determines the height, in twips, of the page, or in whatever measurement value you want to use. The TextHeight property determines the full height of a printed character (including leading, which is the space directly above and below characters). TextHeight measures the height in twips if you haven't changed the scale using the ScaleMode property.

For printed reports, you'll rarely use the ScaleMode method. If you need to change the scale of measurement, however, you'll have to change the scale back to twips before calculating the number of output lines per page, like this:

Printer.ScaleMode = 1

ScaleMode accepts values defined in Table 16.3.

Table 16.3. The ScaleMode values.

Value Named Literal Description
0 vbUser A user-defined value
1 vbTwips Measured in twips (the default)
2 vbPoints Measured in points
3 vbPixels Measured in pixels (the smallest unit addressable by your printer)
4 vbCharacters Measured in characters (120¥240 twips)
5 vbInches Measured in inches
6 vbMillimeters Measured in millimeters
7 vbCentimeters Measured in centimeters

Listing 16.4 contains code that prints two messages, one per page of printed output.

Example 16.4. Moving to the top of new output pages.

1: Printer.Print "The Report begins on the next page..."
2: Printer.NewPage  ' Go to top of new page
3: Printer.Print "The Campaign Platform"

Share ThisShare This

Informit Network