Printer Object - A Primer
The focus of my article is to demystify the printer object and present it as a magnificient object, which can be used to churn out dashing printouts without the support of any third party reporting tool.
Original Author: Gajendra S. Dhir
Code
All programmers creating software solutions for their client, invariably have to process data and generate output on paper, using the printer, in the form of reports. There are many third party tools available in the market which are I, too, have used such report writers, until recently, for my even my most Most literature on Visual Basic, including books and articles, generally explore The focus of my article is to demystify the The sub-topics covered in the article include... Windows operating system allows you to install more than one printer. One of VB provides us with the The To get a list of all the printers installed we could use a code snipet, like or The The entire discussion here uses the The next thing that you must do is setup the dimensions of the paper on which The more commonly used paper sizes are... Please refer to the Microsoft documentation for a complete list of paper size To use a custom size paper your code will look something like... Any attempt to alter the height or the width of the While you are at it, you may also want to setup the orientation of the paper. Any time during the print session you want to check the dimensions of the paper While printing a page a typical use for the height is to compare the paper Note: Depending upon the printer driver installed for the printer Printing to the In VB, this is accomplished by invoking the This method instructs the When you have completed printing all the text and graphics that required to This terminates a print operation and releases the document to the printer. There will be occasions when you may want to abort the print session. This For such times we have been provided with the The difference of the If Print Manager isn't handling the print job, some or all of the data may We can get or set the position using the two properties, Alternately, you may use these very functions to position the printer head Remember 1 inch = 1440 twips. so this previous code snipet should position Every print instruction issued to place text or graphic on the page moves the To print use... notice the ; (semi-colon) at the end of the print statement. in this case note the , (comma) at the end of the print statement. Justification is accomplished with the help of two methods of the So in this example... Let us see print this as Left, Right and Center Justified. Left Justified is the simplest form of justification and the head position The printer head automatically moves to the starting point on the next line Lets try right justification. We have Similarly, you can achieve center justification This is all there is to printing text. Ah yes ... just one more thing before we proceed. The above logic assume that For those interested, here's the entire code, A wide variety of fonts, also known as typefaces, are available under the Windows The To get a list of the names of the fonts available you can use a loop like this... or better still you could use the to get a complete list of the fonts available with each To determine or alter the size of the text that is being printed you must access Other than this, control for Bold, Italic, Underline and The following code will give you a printout of all the printer fonts installed When working with the fonts you can also use Printing in color adds to the presentation value of the final output. Let us Use the Depending on the printer installed, when you the set the printer to vbPRCMMonochrome Once you have activated color printing you can control the color of the output Visual Basic has provided color constants for the standard colors, namely Here are some tips which I think you will find useful during your exploration I welcome and will appreciate constructive feedback and creative suggestions.
Gajendra S. Dhir
Team Leader
Data Spec
Bilaspur-CG, INDIA
instrucmental in generating beautifully crafted reports.
simple printing requirements. That is until I discovered the power of the printer
object.
this printer
object superficially and this, I believe is, why most
of us tend to overlook this simple yet powerful printing tool.printer
object and
present it as a magnificient object, which can be used to churn out dashing
printouts without the support of any third party reporting tool. For detailed
syntaxes of the objects, statements, commands, properties and methods used here
you are requested to refer to the excellent documentation provided by Microsoft.
Select the printer
these is marked as the default printer and is offered as choice for printing
by the applications. Printers
collection and the Printer
object to take care of our printing requirements.printers
collection contains a list of the printers installed
on your system. Printers.Count
specifies the number of printers
installed and any printer can be selected as Printers(i)
, where
i
is a number between 0
and Printer.Count-1
.
this...For i = 1 to Printers.Count - 1
Printer.Print Printers(i).Name
Next i
Printer.EndDocFor Each P in Printers
Printer.Print P.Name
Next P
Printer.EndDocPrinter
object represents the printer which has been marked
as the default printer in the Windows environment.printer
object and can
easily be modified to use the Printers(i)
object.Setup Page Dimensions
you will be printing. Windows has 41 predefined paper sizes based on the standard
paper sizes available around the world. Other than these if the size of the
paper does not match any of these pre-defined sizes you may set it the custom
size and specify your own height and width for the paper. The properties used
here are Printer.PaperSize
, Printer.Height
and Printer.Width
. Printer.PaperSize = vbPRPSLetter
or Printer.PaperSize = vbPRPSA4
constants. Printer.Height = 10 * 1440  '
10 inch height x 1440 twips per inch
Printer.Width = 5 * 1440 ' 5
inch height x 1440 twips per inchprinter
object,
automatically changes the Printer.PaperSize
to vbPRPSUser
. Printer.Orientation = vbPRORPortrait
or
Printer.Orientation = vbPRORLandscape
size you can refer to the height
and width
properties
for the printer
object.
length with current position of the printer head and determine whether the next
line can be printed on the same page or you should request for a new page.
it may or may not report an error is any of the printer properties is set beyond
the acceptable range.Change to a new page
printer
is done in page mode, i.e. the printer
object sends data for printing to the operating system only after it is informed
that the current page formatting is complete and is ready for printing. NewPage
method like
this... Printer.NewPage
printer
object to end the current page
and advance to the next page. End of Print Job
be printed in this print job the printer
object must be so informed.
You can do so using the EndDoc
method.Printer.EndDoc
If something has been printed on the current page it automatically issues a
Printer.NewPage
to complete printing of the page. If a Printer.NewPage
has been issued just before the Printer.EndDoc
method, no blank
page is printed.Cancel the Print Job
may be in response to a cancel request from the user or any such situation requiring
you to do so.KillDoc
method.Printer.KillDoc
KillDoc
and the EndDoc
methods
is more apparent when the operating system's Print Manager is handling the print
jobs. If the operating system's Print Manager is handling the print job KillDoc
deletes the current print job and the printer receives nothing.
be sent to the printer before KillDoc
can take effect. In this
case, the printer driver resets the printer when possible and terminates the
print job.Position the Head
Printer.CurrentX
and Printer.CurrentY
. As obvious by their names the return the
position on the X and Y axes respectively.Label1.Caption = "(" & Printer.CurrentX & ", " & Printer.CurrentY & ")"
as per your requirement.Printer.CurrentX = 1440
Printer.CurrentY = 1440
the printer head 1 inch from each the top and left margins. Similarly this next
code snipet here will position the printer head at the center of the page (half
of width and height).Printer.CurrentX = Printer.Width / 2
Printer.CurrentY = Printer.Height / 2
CurrentX
and CurrentY
and should be considered and,
if necessary, taken care of before issuing the next print instruction.Print out the text
Printer.Print "Text to Print"
Printing starts at the location marked by the CurrentX
and CurrentY
.
After the text as been printed the values of the CurrentX
and CurrentY
are changed to the new location. The new location is different when a , (comma)
or a ; (semi-colon) is added at the end of the Print
statement.
Run the following code and compare the results...
Code 1 Printer.CurrentX = 0
Printer.CurrentY = 0
For i = 1 to 5
Printer.Print Printer.CurrentX & ", " &
Printer.CurrentY
Next i
Code 2 Printer.CurrentX = 0
Printer.CurrentY = 0
For i = 1 to 5
Printer.Print Printer.CurrentX & ", " &
Printer.CurrentY;
Next i
and Code 3 Printer.CurrentX = 0
Printer.CurrentY = 0
For i = 1 to 5
Printer.Print Printer.CurrentX & ", " &
Printer.CurrentY,
Next iJustification - Left, Right or Center
printer
object, viz Printer.TextHeight(Text)
and Printer.TextWidth(Text)
,
with which we can determine the about of vertical and horizontal space that
will be occupied when you print the Text
.mTxt = "Gajendra S. Dhir"
TxtWidth = Printer.TextWidth(mTxt)TxtWidth
is the amount of horizontal space required by the text
in mTxt
to print.'to leave 1" Margins on the Left, Right and Top of the Printer
Printer.CurrentX = 1440
MaxWidth = Printer.Width - 1440*2
Printer.CurrentY = 1440
is already set.Printer.Print mTxt
as there is no comma or semi-colon at the end of the Print
. CurrentY
set for
the next print statement. We need to set the CurrentX
. Now we will
require the MaxWidth
and TxtWidth
values, which we
have ready with us (above).' add 1440 is to maintain the 1" Left Margin.
Printer.CurrentX = 1440 + (MaxWidth - TxtWidth)
Printer.Print mTxtPrinter.CurrentX = 1440 + (MaxWidth - TxtWidth)/2 'again
1440 is to maintain Left Margin.
Printer.Print mTxt
TxtWidth < MaxWidth
. If the width of the text is greater than
the maximum print width then you must separately process the text to either
truncate it so that it fits the MaxWidth
or split the lines suitably
to simulate word-wrap. mTxt = "Gajendra S. Dhir"
TxtWidth = Printer.TextWidth(mTxt)
'to leave 1" Margins on the Top, Left and Right of the page
Printer.CurrentY = 1440
Printer.CurrentX = 1440
MaxWidth = Printer.Width - 1440*2
'Left Justified - no extra work
Printer.Print mTxt
'Right Justified
Printer.CurrentX = 1440 + (MaxWidth - TxtWidth) ' add 1440 is to
maintain the 1" Left Margin
Printer.Print mTxt
'Center Justified
Printer.CurrentX = 1440 + (MaxWidth - TxtWidth)/2 'again
1440 is to maintain Left Margin.
Printer.Print mTxt
'Terminate Printing
Printer.EndDoc Font Name, Size and Style
operating system. Some are optimized for better screen appearance while others
are designed with the printed output in mind. The printer that you use also
has certain built-in fonts which you can access from your VB program.Printer.FontCount
property tells you the number of fonts that
are available in your system and are supported by current the printer. You can
select the name of the font that you want to use for printing your text from
the Printer.Fonts
collectionFor i = 0 to Printer.FontCount-1
Printer.Print Printer.Fonts(i)
Next iPrinter.Font.Name
property like
this...For i = 0 to Printer.FontCount-1
Printer.Font.Name = Printer.Fonts(i)
Printer.Print Printer.Font.Name
Next iFont.Name
printed using that very typeface.
the Printer.Font.Size
property. Mayby something like this...mSize = Printer.Font.Size
Printer.Font.Size = mSize + 4
Printer.Print "THE TITLE TEXT"
Printer.Font.Size = mSize
Strikethru characteristics of a font that are available at your disposal
as a Visual Basic programmer. These are boolean properties and take the values
True
or False
. You may use these properties as... Printer.Font.Bold = True
to enable and False
to disable
Printer.Font.Italic = True
to enable and False
to disable
Printer.Font.Strikethrough = True
to enable and False
to disable
and
Printer.Font.Underline = True
to enable and False
to disable
on your system along with the "bold" and "italic"
texts printed next to the font name.With Printer
For i = 0 to .FontCount-1
.Font.Name = Printer.Fonts(i)
.Print Printer.Font.Name; 'Note
the ; (semi-colon) at the end of print
.Font.Bold = True
.Print " Bold"; 'Note
the ; (semi-colon) at the end of print
.Font.Bold = False
.Font.Italic = True
.Print " Italic" 'Note
no ; (semi-colon) at the end of print
.Font.Italic = False
If Printer.CurrentY + Printer.TextHeight("NextLine")
> Printer.Height - 720 Then
Printer.NewPage
End If
Next i
End Width
'Terminate Printing
Printer.EndDoc
.FontName
, .FontSize
,
.FontBold
, .FontItalic
, .FontStrikeThru
,
.FontUnderline
for .Font.Name
, .Font.Size
,
.Font.Bold
, .Font.Italic
, .Font.Strikethrough
,
.Font.Underline
used above.Print in Color
add some color to our printing. Printer.ColorMode
to enable or disable color printing for your color printer.Printer.ColorMode = vbPRCMColor
or
Printer.ColorMode = vbPRCMMonochrome
prints in shades of black and white.
through two properties two properties, backcolor
and forecolor
,
of the printer
, to control the color of the background and the
foreground respectively. The color values can be assigned to these properties
using the RGB()
function.Printer.ForeColor = RGB(255, 0, 0) ' For
Text in Red Color
Printer.Print "This text is in Red ";
Printer.ForeColor = RGB(0, 0, 255) ' For Text in
Blue
Printer.Print "and this is in Blue"
Printer.BackColor = RGB(255, 255, 0) ' For Background in Yellow
Printer.Print "The text here is Blue and the background is Yellow"vbBlue
,
vbRed
, vbGreen
, vbMagenta
, vbCyan
,
vbYellow
, vbBlack
and vbWhite
.Points for Consideration
of the printer
object...
justified within a maximum width that you may specify. This will allow you
to create the columns in a tabular report and adequately justify the text
within the column.
to enable word wrapping. See my previous code submitted titled
Split Strings for Word Wrapping.
and PictureBox Control. The difference is only in methods like EndDoc
,
KillDoc
, Cls
etc. Using code like...
If Destination = "Printer" Then
Set objDC = Printer
Else
Set objDC = Picture1
Endif
objDC.Print "Hello! This is Gajendra"
you can easily create a print preview.
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.