Sei sulla pagina 1di 12

Printing with Visual Basic

Printing Overview
One of Visual Basics biggest weaknesses is its printing facilities
There are three main ways of printing
Use the PrintForm Method to print a complete form
Use a Printer Object, this is very code intensive
Use a third party application e.g Crystal Reports
Well overview each of these and look at what techniques can be
used to improve the quality of the print
PrintForm Method
The easiest to implement
Produces a complete picture of the form as displayed on the
screen
As it produces a graphics image it can be large in size and hence
slow to print
Syntax
FormName.PrintForm
e.g.
Form1.PrintForm
You can place the PrintForm method under a command buttons
click event or any other event
PrintForm Method - Usage

It is a good idea to hide controls which you do not want printed,


then make them visible again after the Print Method command
has been used e.g.

Sub Command1_Click ()

Command1.Visible = False
Command2.Visible = False

Form1.PrintForm

Command1.Visible = True
Command2.Visible = True
End Sub

Form on Screen Printed Form


Printer Object - Co-ordinates
The Printer object is accessed with the keyword Printer. It is used
to control text and graphics printed on a page and to send output
directly to the default system printer.
It requires the user to specify where on the page items are to be
printed using x and y co-ordinates e.g
Printer.CurrentX = 0 Left most side of page
Printer.CurrentY = 0 Top of page
Printer.Print Wolves Text to be printed
Would print the string Wolves in the top left hand corner of the
printed page
Unless your text is to go on the nect line you need to specify the
co-ordinates
Printer Object - Font Styles

You also need to specify the look of your font in terms of size,
style and whether bold, italics etc. It has properties similar to
controls, look them up in help
e.g.
Printer.FontSize = 12
Printer.FontUnderline = True
Printer.FontBold = True
Printer.FontItalic = True
Printer.Print This is Complex
Would print This is Complex in size 12 font, underlined, bold and
in italics
As you can see to do a good quality report it takes a lot of
planning and a lot of code
Printer Object - Font Styles

Good quality print outs require lots of code to


Set the co-ordinates for where on the page to print
Setting up the look of the font
Setting up how we want the printer to draw using the Draw.. properties or
set up the size and scaling of the page
all this without doing any graphics like lines or circles

There are lots of properties and methods which a


programmer must get familiar with
and this is before we send it all to the printer using
the method at the end of the code i.e.
printer object properties and methods to set layout of page
Printer.EndDoc Sends output to printer
Printer Object - Example

Width of msg with current printer font


Sub Command1_Click ()
Dim HWidth, HHeight, I, Msg ' Declare variables.
On Error GoTo ErrorHandler ' Set up error handler.
Msg = "This is printed on page"
For I = 1 To 2 ' Set up two iterations.
HWidth = Printer.TextWidth(Msg) / 2 ' Get half width.
HHeight = Printer.TextHeight(Msg) /2 ' Get half height.
X Co-ordinatePrinter.CurrentX = Printer.ScaleWidth / 2 - HWidth
of print start Printer.CurrentY = Printer.ScaleHeight / 2 - HHeight Co-ordinate of
on paper the paper side
Printer.Print Msg & Printer.Page & "." ' Print.
edge
Printer.NewPage Page number ' Send new page.
Next I
Printer.EndDoc ' Print done.
Printer Object - ExamplE (contd)
Msg = "Two pages, each with a single, centred line of text, "

Msg = Msg & "have been sent to your printer."


MsgBox Msg ' Display message.
Exit Sub
ErrorHandler:
MsgBox "There was a problem printing to your printer."
Exit Sub
End Sub

Demo its building on PC, show example


Told you it wasnt easy
Printer Object - Example
(contd)
This page should show an example of the previous
pages print out !!
Third Party Packages for
Printing
Using Crystal reports
Using HTML reports
Third Party Packages for
Printing
Another way to print information is to use Object Linking and
Embedding (OLE)
Here you link to other packages such as Word and using their
Basic commands send information to be displayed in these
packages
You can then send commands for the document to be printed as
you would for any document in your linked package
Well be talking more about OLE in the level 3 module

Potrebbero piacerti anche