Printing
Most Visual Basic applications will use a printer to provide a hard copy of documents and other forms of output produced by the program, whether this consists of text, graphic images, or both. Printing is one of the more difficult programming tasks to achieve in Visual Basic, and there are two main ways to print. The first method is to put the required output into a form and then print the form using the PrintForm method. The second method is to send text and graphics to the printer object, and then print them using the NewPage and EndDoc methods. Both techniques use the system's default printer (it is also possible to select a printer in Visual Basic, but this will be dealt with in a later tutorial).
Printing a Form
The PrintForm method sends an image of the specified form to the printer. The PrintForm command is usually invoked by a procedure associated with either the form itself or one of the controls on the form, and prints the entire form. If the form contains graphic elements, these too will be printed if the form's AutoRedraw property is set to True. Although this is the easiest way of printing from an application, the results may be disappointing, since the form is reproduced in the resolution of the screen, not the printer. The screenshot above shows the "Goats 'R' Us" program. The code for the program is shown below.
Option Explicit
Dim sngDailyRate As Single
Dim sngNumberOfDays As Integer
Dim sngHireCost As Single
Dim strLastName As String * 20
Dim strFirstName As String * 20
Private Sub Calculate_Click()
strLastName = CustLastName.Text
strFirstName = CustFirstName.Text
sngNumberOfDays = Format(Days.Text, "General Number")
sngHireCost = sngNumberOfDays * sngDailyRate
HireCost = Format(sngHireCost, "Currency")
End Sub
Private Sub Form_Load()
sngDailyRate = 12.5
sngNumberOfDays = 0
Days.Text = Format(sngNumberOfDays, "General Number")
sngHireCost = 0
strLastName = ""
strFirstName = ""
HireCost = Format(sngHireCost, "Currency")
End Sub
Private Sub PrintForm_Click()
PrintForm
End Sub
Using the Printer Object to Print
Using the Printer object to print in Visual Basic involves somewhat more programming code, but usually provides better results. The Printer object supports a number of built-in methods, such as Print, PSet, CurrentX, CurrentY, Line, PaintPicture (used to print the contents of Picture boxes), and Circle to print text and graphics. When you have finish placing information on the Printer object, the EndDoc method is used to send the output to the printer. The NewPage method allows you to print documents consisting of two or more pages. The Printer object has several properties that control print quality, page size, number of copies, scaling, page numbers, and other features.
The usual approach to using the Printer object is to consider each printed page to be a form with its own coordinate system. The coordinate system is used to position text and graphics on the page. The Scale method allows you to set the units for the coordinate system. In the example below, I have defined a printing area 8.27 x 11.68 inches (a standard A4 sheet of paper) using a scale of 100 units = 1 inch:
Printer.Scale (0, 0)-(827, 1169)
Every item (text or graphics) is subsequently placed on the page using this coordinate system. An example application - "Gremlins4Hire" - that prints a (very) simple invoice is shown below, together with its code.
The Program Code
Option Explicit
Dim sngDailyRate As Single
Dim sngNumberOfDays As Integer
Dim sngHireCost As Single
Dim strLastName As String
Dim strFirstName As String
Private Sub Calculate_Click()
strLastName = CustLastName.Text
strFirstName = CustFirstName.Text
sngNumberOfDays = Format(Days.Text, "General Number")
sngHireCost = sngNumberOfDays * sngDailyRate
HireCost = Format(sngHireCost, "Currency")
End Sub
Private Sub Form_Load()
sngDailyRate = 25
sngNumberOfDays = 0
Days.Text = Format(sngNumberOfDays, "General Number")
sngHireCost = 0
strLastName = ""
strFirstName = ""
HireCost = Format(sngHireCost, "Currency")
End Sub
Private Sub PrintInvoice_Click()
'Declare variables
Dim Counter As Integer
Dim strInvHeader As String
Dim strG4AddressArray(5) As String
Dim strInvLineArray(1) As String
Dim HeaderWidth As Integer
Dim AddressPos As Integer
'Set printing area for A4 (100 units = 1 inch)
Printer.Scale (0, 0)-(827, 1169)
'Draw a box around the body of the invoice
Printer.Line (100, 100)-(727, 1069), , B
'Draw the Gremlins4Hire logo
Printer.PaintPicture Me.Picture, 120, 120, 200, 200, 0, 0, , , vbSrcCopy
'Initialise string array variables
strInvHeader = "Invoice"
strG4AddressArray(0) = "Gremlins4Hire"
strG4AddressArray(1) = "Creepy House"
strG4AddressArray(2) = "Fiend Street"
strG4AddressArray(3) = "Plymouth"
strG4AddressArray(4) = "Devon"
strG4AddressArray(5) = "PL2 2EE"
strInvLineArray(0) = "Customer Name: " & strFirstName &
" " & strLastName
strInvLineArray(1) = "Invoice total: " &
sngNumberOfDays & " days @ £25.00/day = " & HireCost
'Set font parameters (Arial 36pt, bold, underlined)
Printer.FontName = "Arial"
Printer.FontSize = 36
Printer.FontBold = True
Printer.FontUnderline = True
'Print invoice header
HeaderWidth = Printer.TextWidth(strInvHeader) / 2
Printer.CurrentX = Printer.ScaleWidth / 2 - HeaderWidth
Printer.CurrentY = 150
Printer.Print strInvHeader
'Reset font size to 12 point and turn off bold and
underline attributes
Printer.FontBold = False
Printer.FontUnderline = False
Printer.FontSize = 12
'Print Gremlins4Hire address
Printer.CurrentY = 150
For Counter = 0 To 5
AddressPos =
Printer.TextWidth(strG4AddressArray(Counter)) + 120
Printer.CurrentX =
Printer.ScaleWidth - AddressPos
Printer.Print
strG4AddressArray(Counter)
Next
'Print customer name
Printer.CurrentY = 500
Printer.CurrentX = 200
Printer.Print strInvLineArray(0)
'Print invoice summary
Printer.CurrentY = 600
Printer.CurrentX = 200
Printer.Print strInvLineArray(1)
'Send page to printer
Printer.EndDoc
End Sub
Private Sub Quit_Click()
Unload Me
End Sub