Visual Basic
  Home arrow Visual Basic arrow Page 2 - Printing With Visual Basic
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC

Printing With Visual Basic
By: James Crowley
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 199
    2002-04-07

    Table of Contents:
  • Printing With Visual Basic
  • Printing with VB
  • Printing graphics
  • More Printing in VB
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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

    Printer.Print "Hello"

    Each time you call the Print method, your output will move to the next line. So,

    Printer.Print "Hello"

    Printer.Print "Goodbye!"


    would appear as

    Hello

    Goodbye!


    on paper.

    If you want to output a new page, you simply call the NewPage method:

    Printer.NewPage

    Once you have finished outputting the data to the printer, you need to call the EndDoc method:

    Printer.EndDoc

    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:

    With Printer

    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"

    .EndDoc

    End With


    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.

    Printer.CurrentX = Printer.ScaleWidth / 2 - HWidth

    Printer.CurrentY = Printer.ScaleHeight / 2 - HHeight

    Printer.Print sMsg


    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.

    More Visual Basic Articles
    More By James Crowley


     

    VISUAL BASIC ARTICLES

    - Developing an XML Web Service Using Visual S...
    - Creating an HTML File List with VB
    - Fun with Email: VB6, CDO, MAPI, and a Remote...
    - Extranet/Intranet Dictionary Cracker in VB
    - Finding Default App Icons With Visual Basic
    - Registry Fever With Visual Basic
    - Implementing An ADO Data Control With VB6
    - Printing With Visual Basic
    - MSMQ Part 1/2: Architecture and Simple Imple...
    - Magnifying The Desktop With Visual Basic
    - Sending Email With MAPI Components in Visual...
    - Two Person Chat With The Winsock Control And...
    - A Real-Time ActiveX News Control
    - Accessing the Windows API in Visual Basic







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway