Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
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"
Summary | Next Section

Account Sign In
View your cart