Despite claims that computers would create a "paperless society", printing is still one of the most important functions of any application. Visual Basic makes it easy to print, and in this article James shows you what you need to know to print text, shapes and images with Visual Basic. If you need to create your own invoicing or reporting system with Visual Basic then this article is a definite must read.
Printing With Visual Basic - Printing with VB (Page 2 of 5 )
Unsurprisingly, Visual Basic provides us with the Printer and Printers objects, which are available at runtime to all VB apps. The Printers object gives us information about all the available printers installed on the system. The Printer object controls all the output to the active printer and retrieves information such as the printer name.
First of all, lets look at the basic steps involved in printing with VB. First off, we send all the data we want to print using the Print method. For example, if we wanted to print "Hello", we would call
Each time you call the Print method, your output will move to the next line. So,
would appear as
If you want to output a new page, you simply call the NewPage method:
Once you have finished outputting the data to the printer, you need to call the EndDoc method:
Windows will then spool your document to the printer.
Setting formatting options
Using the Printer object further, you can also apply formatting to the text you output by setting the Font and its associated FontSize, FontBold, FontItalic, FontStrikeThru and FontUnderline properties. To change the font, simply change the FontName property. For example,
Printer.FontName = "Tahoma"
changes the font to Tahoma. To change the size, just call Printer.FontSize. Most of the other formatting options are just boolean (ie True or False), which makes it even easier to apply formatting. The code below gives a simple demonstration:
Printer.Print "Normal Line"
.FontBold = True
Printer.Print "Bold Line"
.FontItalic = True
Printer.Print "Bold and Italic Line"
.FontBold = False
.FontItalic = False
Printer.Print "Second Normal Line"
Although I am using a With statement, I still have to write Printer.Print for outputting the text. This is another 'quirk' of VB - the Print statement does not actually belong to the Printer object, or in fact any other object in VB. If you search for Print in the Object Browser (press F2), you won't find it anywhere. Print is in fact a statement that VB interprets on its own, and outputs it according to the object before the Print statement. If you call PictureBox1.Print, it will 'print' the text into the PictureBox control. As such, the Print function behaves just as if it were an object of PictureBox or Printer. However, the designers of VB obviously didn't extend this behavior to With statements.
For a full list of font options, go to the Object Browser (F2), and select Printer from the Classes list.
One other thing worth noting regarding formatting... If you are using the RichTextBox control, you don't need to worry about applying all the formatting - there is some code using API calls which does it all for you, including page margins! Click here to see that code.
Setting the alignment of text isn't quite so simple because VB doesn't provide an align property. Instead, we need to work out where the middle of the page is and then take into account the width of the text. Take a look at this code, which prints the page number in the middle of the page:
sMsg = "Page " & Printer.Page ' Print Page number
HWidth = Printer.TextWidth(sMsg) / 2 ' Get one-half width.
HHeight = Printer.TextHeight(sMsg) /2 ' Get one-half height.
This code uses the CurrentX and CurrentY variables to change the position of where the text is outputted, centers the position, and prints the page number using the Printer's Page property. If you want the text in the middle at the top of the page, simply comment out the Printer.CurrentY line.