Library tutorials & articles

Printing in VB

Printing Graphics

Outputting graphics to the printer is identical to outputting graphics to a PictureBox. We will cover a few basic commands here, but first lets take a quick look at the CurrentX and CurrentY properties. These were used in the last example, and show where the current coordinates are. Visual Basic automatically change these when it has finished the outputting. For example, imagine you draw a line on a piece of paper - when you have finished drawing, the pen is at the end of the line, not the start. This is the same with the CurrentX and CurrentY properties. If you call the Line method, the line is drawn, and then CurrentX and CurrentY are changed to point to the end of the line. You can then change these if necessary to control where the next text or line is printed.

Note
VB uses the same functions for drawing in picture boxes and outputting to the printer. If you don't want to have to visit the printer and waste paper whilst following this tutorial, simply create a new project, set the forms backcolor to white, autoredraw property to true, and remove change any Printer.Function to Function. You will also need to comment out the Printer.EndDoc lines. The data that was going to be printed on paper will now be 'printed' to the form instead.

Comments

  1. 12 Oct 2008 at 16:29
    How do i get vb.net to print. i've got it to make the printer work but it prints a blank page. Thanks
  2. 27 Sep 2005 at 09:43
    Can you tell me how to set the margins and printer orientation also using this method.  It should work in VB .NET 2003 I'm assuming.

    I need to be able to control the printer margins and headers and footers and page orientation so that everything prints out the way I want.

    Please advise me if you know a solution.  

    Thank you,

    Baku
  3. 27 Sep 2005 at 05:35
    [Hi,

    Facing problem in printing from local printer connected to WinXp from VB6. I am able to capture Printer.devicename and thru this print to network printer. But when trying to print on Local printer , No error appaers but "NO Document is printed". \

    Can anyone help. Its very urgent ..PLsssssssss

    -
    VB
  4. 04 Jul 2005 at 15:32

    hi suhaas


    have u added new paper...


    now go to properties of your generic printer..
    in printing preferences select default paper instead of letter select ur created custom paper...


    now open vb6 and write code "msgbox printer.papersize"
    this will give u paper no which windows has assigned...


    so now when writing ur code..
    instead of


    printer.papersize=256
    printer.width=your width
    printer.height=your height



    replace 256 with ur custom paper no...
    and height and width will be automatically adjusted as per you custom paper created...


    printer.papersize=your custome paper size no



    like this u can solve it..


    i hope things are clear


    if more doubts feel free to ask

  5. 04 Jul 2005 at 13:51

    hi aadreja,


    thanks for your reply.


    I tried to do as per your reply. But this does not work with VB6. I am working on a invoice printing application where I am using Generic Text printer with VB6. Please help me if you have any solution.


    Thanks in advance.


    Regards,
    Suhaas

  6. 04 Jul 2005 at 09:51

    for setting custom paper size in xp
    u have to create form...


    go to start->settings->printers
    in file menu go to "server properties"
    create new from with desired paper size... and give name


    now u can see newly created paper while printing

  7. 24 Jun 2005 at 07:03
    email me

    I Am facing problem with printer.print when i use in Win Xp. setting paper size have no effect when use it in xp but its working correctly  in win 98

    have seen this question in this forum but can't find the replies

    hope you can help me, i am using vb6

    thanks.

    email me
  8. 18 Jun 2005 at 00:31

    I am attempting to print to a 'HP LazerJet 2420d' printer that has duplex capabilities but I can not get it to print the back side of the paper.  I am coding in VB6 and I know the duplex on the printer works because another program that I did not write works fine. Any help?  I've tried everything I can think of.

  9. 04 Jun 2005 at 15:42

    I've developed a compontent to make printing in VB easier.
    Some common tasks as printing images, wrapped text, objects, etc. are coded on the component's methods.
    Test it for yourself. Visit http://www.rc-dev.com

  10. 16 May 2005 at 16:15

    Thank you.... I understand it better now.  I guess I didn't realize how many ways you can accomplish the same things in VB with different code.  For instance, the code you use to connect and query a database is different than mine, but its more clear now that I see the whole thing.  Thanks for the help!  

  11. 14 May 2005 at 15:41

    I'm currently developing a DLL to make printing easy in VB. Next week I'll relese the Beta1 version of this module.
    If you like to post some suggestions that you find usefull, email suggest@rc-dev.com .

  12. 14 May 2005 at 15:35

    Well, a recordset is a coolection of records (rows) of a db table.
    So, when you open a recordset with a Sql query, it get's filled with the query results (or not filled at all if the query dosen't return any results.
    But if it does get filled, you must move along the returnd rows to get their data.


    example:

    Code:

    set rec=new adodb.recorset
    set conn=new adodb.connection


    conn.open "your connection string" 'do a google search ob ado connection strings if you don't know what this is.
    'also check my site's code lib. I posted there a function to build connection strings easy.


    rec.open "select * from myphonebook order by name,phonenr", Conn ' load query results into the recordset
    do while not rec.eof 'loop through all the results
      printer.print rec.fields("name") & vbTab & rec.fields("phonenr")
      'print the name and phone nr. of the current row separated by a TAB
      rec.movenext 'move to the next row on the recordset
    loop
    rec.close 'close the recordset
    conn.close 'close the db connection


    set rec=nothing 'destroy the object
    set conn=nothing 'destroy the object


    printer.enddoc 'start printing



    hope it helps

  13. 13 May 2005 at 14:28
    Hi, I am also trying to print a table in VB.net, but I seem to have less experience than gianni because I'm having trouble understanding your directions.  I have a few questions after reading your solution.

    First, is the recordset the same thing as a dataset?  Or, could I load the contents of the table into a dataset and print from the dataset?  If I can't print from a dataset, do I load the information from the dataadapter into the recordset in the same way that I would put the information into a dataset?  

    Finally, I'm having trouble understanding the loop.  Let me know if this is incorrect:


    do while not rec.EOF                                                                       <---- Lop until you reach the end of record set?
     printer.print rec.fields("name")  & vbTab & rec.fields("phone_nr")     <---- I have no idea....
    rec.Movenext                                                                                 <---- Does this increment some kind of index in the recordset that indicates the current
    loop                                                                                                       record?  


    Thank you for any help you can provide.

    jglennie

  14. 23 Apr 2005 at 11:38

    Dear Sharaf / Forum Members,


    I am also facing similiar problem with VB6. I am using Generic Text Printer in Windows 2000. I want to assign Custom paper size to the printer but it is not working. It works with Win98. Have you got the solution? If yes please forward me.


    Thanking you in advance.

  15. 13 Apr 2005 at 20:21

    excellent thanks!

  16. 13 Apr 2005 at 19:27
  17. 13 Apr 2005 at 09:36

    can one add the functionality to print a  logo  easily when printing directly to the printer ?


    I want to add a logo to till receipt slips (small thermal printer) and just wondering how it could be done

  18. 07 Apr 2005 at 18:44

    do while not rec.EOF
      printer.print rec.fields("name")  & vbTab & rec.fields("phone_nr")
    rec.Movenext
    loop


    don't know if this is what you mean...

  19. 07 Apr 2005 at 14:48

    May i have the code to print the contents through the recordset? ( I already loaded the table into the recordset)

  20. 06 Apr 2005 at 20:51

    You cannot do it directly. You must load the table nito a recordset and then print the contents.


    To position printing:


    Printer.currentX=...
    Printer.currentY=...


    To print:


    Printer.print "your data"


  21. 06 Apr 2005 at 17:33

    Can someone please tell me how can i print the contents of a database's table using the printer.print command?

  22. 26 Jan 2005 at 22:24

    Hello,


        I just about to develop a window application to print to Printronix.  I don't have the answer to you question but is hoping you can give me some insights.  Where did you get the driver and documentation for Printronix Printer ?  I am looking for both driver and documentation on how to utilize the DLL that comes with the driver.  Please help.



    Thanks

  23. 14 Dec 2004 at 19:05
    Print Preview:
    webDoc.ExecWB OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_PROMPTUSER, Nothing, Null
    Print:
    webDoc.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, Nothing, Null
  24. 14 Dec 2004 at 19:01
    You can use the following statement in vb to remove the headers and footers:
    Print:
       webBrowser.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, Nothing, Null
    Print Preview:
        webBrowser.ExecWB OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_PROMPTUSER, Nothing, Null
  25. 26 Oct 2004 at 23:35

    Would like to know if u have any solution for that? Cos I am facing the same problem for winxp.

  26. 08 Jul 2004 at 07:55

    Am facing problem with printer.print when i use in Win Xp. setting paper size have no effect when use it in xp but its working correctly  in win 98

  27. 02 Jul 2004 at 10:08

    I am beginner programmer, therefore I put in one problem that I discribe you following :-


    I am working on one project named Bill Software in this software user need printing the form. Therefore I do progrmming that (form4.printform) but it display correctly on my printer "HP 4110" and when I make distribution CD of my project and setup my software on user computer then it work properly but its printing not became correct. My user have "Dot Matrix Printer".
    Please tell me what I do?
    I know that this problem is very small but I am beginner, so I face like that problem.
    I also try to use MSDN help but I can't get my problem solution.


    Summary :- I want to print the form on (InkJet Printer & also on Dot Matrix Printer) without using my user computer for programming. I know that I maintain that problem by using user computer for programming but Sir I have 10 order of my Bill Software. Please Help me immediately.

  28. 24 Feb 2004 at 02:58

    HI
    I want to print the treeview in VB . so how can i print the it in treestructure all the nodes?
    plz comment.
    Regards
    mahesh

  29. 20 Jan 2004 at 11:24

    The tutorial on printing is fantastic, but printing in VB seems a little archaic.  Is there anyway to use the web browser control to print an html rendered document without the lovely header and footer and without asking the user to remove these?  One way would be to alter the header and footer in the registry settings; however, this (as I know) will not work for sub-power users.

  30. 19 Dec 2003 at 00:46

    does someone know a way to render a word document over a specific DC?


  31. 10 Dec 2003 at 07:14

    I have problem with Print in Printronix Printer ( P5000 Series ),
    i can't print more than 2 pages its containing both barcode picture and text.
    I already use printer method like newpage, enddoc
    and i also use Win API Print method just like startpage,endpage,startdoc, enddoc.
    I also try using different printer driver like epson fx1050, printronix P5000 Series,
    But Still i haven't found solution to My problem.
    Is there anybody in this forum who cant help me ???



    Thanx.


  32. 06 Nov 2003 at 00:26
    btw, if you're using an IE browser, see if this will batch print *.docs, & *.xls. since they can open directly in IE.  Would be neat if it did - let me know.
  33. 06 Nov 2003 at 00:21
    I guess I could, but I don't think it's really of any value...I just did it b/c I had to print about 30 htm files and I didn't want do the file...print crap each time.  I started working on a simple html rendering engine so that I don't have to use MS' print function ( it forces the dialogue box- but still easier than mousing through everything), but never completed it.  
    '******************************
    'After IE 4.0, the web browser object doesn't allow the bypassing of the print dialogue.
    'Have to use webBrowser control until html engine can be rendered because VB Printer object prints html source not rendered.

    Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, _
    Shift As Integer, x As Single, y As Single)
       Dim i As Long
       Dim a As Integer
       Dim s As Integer
       Dim URL As String
     For i = 1 To Data.Files.Count
           s = List1.ListCount
           List1.AddItem Data.Files(i)
     Next i
     End Sub
    Private Sub CmdPrint_Click()
    Dim doc As Object
    Dim win As Object
     Do While List1.ListCount <> 0
         URL = List1.List(s)
         WebBrowser1.navigate (URL)
         MsgBox ("Ready to Print") ' Needs timer here to wait about 2 seconds for url to load before print.
           Set doc = WebBrowser1.document
           Set win = doc.parentWindow
           win.execScript "window.print();", "JScript"
         
         MsgBox ("Press this after you OK print dialogue")
         ' Need timer control (MsgBox) to pause next event because of printer dialogue loses page.
         List1.RemoveItem (s)
         Loop
       List1.Clear
    End Sub
    Private Sub FrmMain_Load()
    Dim objPrinter As Printer
    ' Not grabbing device name for some reason, but I didn't really debug it yet.
       'Set objPrinter = GetDefaultPrinter()
       'Label2.Caption = objPrinter.DeviceName
       'Set objPrinter = Nothing
       'End
       List1.Clear
       List1.OLEDropMode = vbOLEDropManual
    End Sub
  34. 05 Nov 2003 at 21:11
    Hey Warren,

    How long is the code?  Could you just post it directly in this forum?

    Thanks.
  35. 04 Nov 2003 at 19:12
    Instead of using a JavaScript.print function to print a web page, is there any code I can use in ASP that I can print a doc within a web.(THE URL usually print at the bottom which I don't know how to get rid of it?)

    Please send the "Print code" to citiloan88@yahoo.com and I appreicate your help....
    Thanks
  36. 04 Nov 2003 at 18:56
    " yeah, I made an app to batch print html documents.  The problem I ran into after it was done, is that html needs to be rendered.  My app could only print the source , just as if it were an rtf,txt, or doc file.  I took the lazy way out and used MS' component for it; which will prompt the print dialog cause MS see's printing as a security risk.  There is no legit way using the component to bypass the dialog box.  I was going to try mimicking the ok, so the user doesn't have to sit there, but got too lazy.  If you want the prog, I'll send you the code. "

    Can anyone please e-mail this PRINT application to me at citiloan88@yahoo.com

    Thanks
  37. 14 Oct 2003 at 05:02
    I am having problem with printing out a document that with paper size (Width:9.5 inch,Height:6 inch). I use "Printer.Print" to print out every line of the document. But I can't stop the printing when finish printing all the line..the paper scrol till to second page and stop at the half page of the second page. How should I control the paper setting by using VB code?
  38. 26 Sep 2003 at 17:23
    reply to my email so I know where to send it, or post it, whichever you prefer.
  39. 26 Sep 2003 at 16:11
    Thanks that will be very helpful to me if you send me the source code.

    Thanks,

    Nutan
  40. 26 Sep 2003 at 15:51

    yeah, I made an app to batch print html documents.  The problem I ran into after it was done, is that html needs to be rendered.  My app could only print the source , just as if it were an rtf,txt, or doc file.  I took the lazy way out and used MS' component for it; which will prompt the print dialog cause MS see's printing as a security risk.  There is no legit way using the component to bypass the dialog box.  I was going to try mimicking the ok, so the user doesn't have to sit there, but got too lazy.  If you want the prog, I'll send you the code.

  41. 25 Sep 2003 at 10:43

    Thank u


    also I hd got a API func for that
    It prints like printing via dos


    AnyWay thnks for your reply

  42. 25 Sep 2003 at 10:41

    Does anyone has solution for this problem, I am also looking out for the same.

  43. 25 Sep 2003 at 04:38

    Speed is slow in case of Printer.Print on DMP


    Use


    OPEN "LPT1" For Output as #1


    Print #1, "JainaSoft Technologies"
    Print #1, "For Complete Software and INTERNET Solution"
    ....


    close #1


    it will give u the good speed on DMP

  44. 13 Sep 2003 at 01:21

    what's ur Solution?


    PLease give me ya.
    I need that

  45. 11 Sep 2003 at 10:22

    Stor the printer name in strPrinterName.


    Dim printerObj As printer



       For Each printerObj In Printers
       
           If UCase(printerObj.DeviceName) = UCase(strPrinterName) Then
       
               Set printer = printerObj
       
               ' do stuff here


               Set printerObj = Nothing
               Exit For
               
           End If
       
       Next




    // johannes

  46. 10 Sep 2003 at 04:20
    Thanks,

    I Have got the solution, there was some mistake in my code so ..., Not using apropriate driver is also the reason of it. So install the original driver always.

    Regards

    Gyanendra
  47. 10 Sep 2003 at 02:40
    How to speed up  the [3].[/3] matrix printer while printing in vb as dos oriented printing
  48. 10 Sep 2003 at 02:21
    Does this help? Use a the WebBroswer control

    In a VB app add the WebBroswer component (MS Internet Controls) WebBrowser1

    To print:

    1) open the file

    WebBrowser1.Navigate  "yourfile.html" or "yourfile.txt" or "http://www.somesite.com"

    2) call the print command

    WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

    My problem is I dont want the page1 that's in the upper right hand corder and the URL at the bottom of the page, as happens whenever you print from a browser.  If you figure how to get rid of this, please let me know.
  49. 09 Sep 2003 at 02:10

    hello, hope my solution will help you.

    do not put printer.enddoc

    this will end the document if youre printing in printers such as the inkjets and laserjets,

    you can change the properties of this printer by:

    printer.orientation
    printer.paperbin

    by this your printer settings will be change, it is important to understand 1st your printers capabilities

    and features. ;)
  50. 09 Sep 2003 at 02:04
    hello

    please try to fix the coordinates of the printer by coding it, you can set its current x and y coordinates. Most new printers have drivers to support and automatic reconfigures the coordinates. Try also to code the papertype, paperbin.

    Hope this will help you!

    Good luck
  51. 02 Sep 2003 at 12:19

    I have this exact problem. If anyone has the solution could you please e-mail it to me.
    macbro2@hotmail.com

  52. 28 Aug 2003 at 12:49

    Hi,
    I am creating a macro in MS Word to do mailmerge and printing automation.  After mailmerged, the merged document should be send to the printer. In the process of sending to printer, I like the users be able to select of what printer they want the document to be printed.  Please help me on the syntax of printer options.  Thanks.

  53. 24 Aug 2003 at 02:06

    I have a problem regarding printing in VB. When I use Printer.Print method followed by Printer.Enddoc method to print a line of text, the paper gets ejected after printing that line. The paper should not get ejected after printing a line and printer should wait for next line of text.
    How to solve this problem?Can enyone help me?

  54. 22 Aug 2003 at 14:37

    Did anybody get any solution for this problem> Please let me know I desperatelly want to implement this.
    I want to print a RTF file which will be around 10K and i want to send the entire file directly to the printer.


    Select a file and send it to printer thru' VB Code.


    I would aappreciate if you could help me in this matter.
    myEmailID g_mandar@hotmail.com


    Thanks,
    Mandar

  55. 18 Aug 2003 at 03:19

    Hi
    i have the same problem.
    please if u solved the problem contact me.
    my email is:leon@galor.com
    best regards!
    Leon

  56. 08 Aug 2003 at 17:46

    Hi all,


    I'm having a trouble figuring out how to pass entire documents directly from my app to the printer (i.e., without opening up Word or IE Explorer).  I have a list of rtf and html docs that I would like to print automatically, without forcing the user to open each document in a different app and print from there.  When the user presses "Print" I would like the ability to take each of the pathnames that I have stored and send them to the printer in succession so that it gives the feeling of seamless printing of the entire list of documents.  Does anyone know how I could accomplish such a task? (hopefully something similar to "printer.Paintpicture(loadpicture("name"), Top, Left, Width Height" for images???).  HTML and RTF are the major doc types that I need to be able to print this way.  Any help would be GREATLY appreciated!

  57. 25 Jun 2003 at 20:53
  58. 18 Jun 2003 at 03:10
    Please tell me how can we print on Dot Matrix Printer using this code "Printer.Print", as i used it but i am not getting proper printout on Dot Matrix Printer while on Ink Jet Printer printout is proper. On Dot Matrix Printer it prints 10-12 characters of each line, it does not print complete line. So please if any one can solve it, please let me know.

    Thanks & Regards
  59. 08 May 2003 at 12:02

    I've find out a great site for VB source code!  Thanks a lot!

  60. 02 Dec 2002 at 17:35

    Is this possible? I can find no method using help or internet forums, but can't help feeling someone must know how.


    Thanks,
    Mick

  61. 19 Aug 2002 at 20:57
    This post helped me get thru a whole bunch of ____... MSDN Library help did not have the quirk you pointed out. Please accept my sincere thanks. Alan.
  62. 20 Jun 2002 at 12:59

    I am just know learning vb, and I'm  trying to figure out how to print from it. Is there any way I can create a macro that  upon clicking it, it displays  a report with the contents of several variables in another macro?
    Please if anybodycan help me, would you email me at soy_chicalatina@yahoo.com
    Thanks a lot!
    Mileidy

  63. 01 Jan 1999 at 00:00

    This thread is for discussions of Printing in VB.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!