Printing
In most cases, one line of code sends a TXTextControl document to the printer. When an application needs more control, such as letterhead paper for the first page or a preview window, it prints the document page by page to a device context of its choice.
The Visual Basic 6 samples start with the one-line print job and a print dialog box, then build a page-by-page printing routine and extend it with paper bins and a print preview.
Print a Document
The PrintDoc method detects the default printer and its settings automatically, so you only specify the range of pages and the number of copies:
TXTextControl1.PrintDoc "My Print Job", 1, _
TXTextControl1.CurrentPages, 1
The code prints one copy of the current document and works as follows:
- The first parameter, "My Print Job", is a name of your choice under which the print manager shows the print job.
- The second and the third parameters are the range of pages to print, here from page 1 to the last page, which the CurrentPages property returns.
- The last parameter is the number of copies to print.
Let the User Choose the Printer and the Pages
A Common Dialog box lets the user select the printer, the range of pages and the number of copies:
' Display a common print dialog
CmDialog1.Copies = 1
CmDialog1.FromPage = 1
CmDialog1.ToPage = TXTextControl1.CurrentPages
CmDialog1.Min = 1
CmDialog1.Max = CmDialog1.ToPage
CmDialog1.Flags = cdlPDHidePrintToFile Or cdlPDNoSelection
CmDialog1.ShowPrinter
' Print selected pages
TXTextControl1.PrintDoc Form.Caption, CmDialog1.FromPage, _
CmDialog1.ToPage, CmDialog1.Copies
The TXWords sample of version 34.0 doesn't need this code: it calls PrintDialog, which opens the system print dialog box and prints the document, as A Word Processor shows.
Print Page by Page
The options of PrintDoc are enough for most applications. When you need more control over printing, for instance to use a different paper bin for the first page than for the following pages, use the PrintPage method.
PrintPage does nothing but send a single page to the printer, which gives you as much flexibility as possible. It does not even eject the printed page, so you can print the contents of several TXTextControl controls on one sheet of paper.
PrintPage has one parameter, the page number, and you call it for each page to print in turn. Selecting the printer and controlling the print job takes some more properties and methods:
For CurPage = 1 To TXTextControl1.CurrentPages
' Initialize the VB Printer object
Printer.Print
' Select a printer
TXTextControl1.PrintDevice = Printer.hDC
' Print one page
TXTextControl1.PrintPage CurPage
' Eject the page
If CurPage <> TXTextControl1.CurrentPages Then Printer.NewPage
Next CurPage
' Tell Print Manager we are finished
Printer.EndDoc
The code works as follows:
Printer.Printinitializes the Visual BasicPrinterobject.- PrintDevice selects the printer by its device context,
Printer.hDC. PrintPageprints one page, andPrinter.NewPageejects it after every page but the last.Printer.EndDoctells the print manager that the print job is finished.
The next two sections extend this basic routine.
Change Paper Bins
A typical requirement of office applications is to print the first page of a document on letterhead paper and the remaining pages on plain paper. For this, you usually use a printer with two separate trays for the two types of paper. The application sends the first page to the first paper bin and then switches to the second bin for the remaining pages.
Two lines that set the paper bin extend the printing code of the previous section:
Printer.PaperBin = vbPRBNLower ' select first tray
For CurPage = 1 To TXTextControl1.CurrentPages
Printer.Print
' select second tray
if CurPage = 2 then Printer.PaperBin = vbPRBNUpper
TXTextControl1.PrintDevice = Printer.hDC
TXTextControl1.PrintPage CurPage
If CurPage <> TXTextControl1.CurrentPages Then Printer.NewPage
Next CurPage
Printer.EndDoc
The code works as follows:
- Before the loop,
Printer.PaperBinselects the first tray,vbPRBNLower. - From page 2 on, it selects the second tray,
vbPRBNUpper.
Show a Print Preview
PrintPage prints not only to printers but to any control that has an hDC property. A Picture Box, for instance, serves as a print preview window: assign its hDC property to the PrintDevice property, and PrintPage prints to the Picture Box instead of the printer.
The following code prints page 1 to a Picture Box named Picture1. The PrintZoom property scales down the printout when the Picture Box is smaller than a full printer page:
TXTextControl1.PrintDevice = Picture1.hDC
TXTextControl1.PrintZoom = 25 ' scale down to 25%
TXTextControl1.PrintPage 1
The TXWords sample program has a full print preview form that works this way. Its source code is in the Samples\vb6\TXWords folder.

Key Members
| Member | Description |
|---|---|
| PrintDoc | Prints a range of pages in a number of copies on the default printer. |
| PrintPage | Prints one page to the device that PrintDevice sets, without ejecting it. |
| PrintDevice | Sets the device context that PrintPage prints to: a printer or a control such as a Picture Box. |
| PrintZoom | Scales the printout, for instance for a preview. |
| CurrentPages | Returns the number of pages, the last page to print. |
The programming category Printing and Print Preview lists all members for printing.