Results 1 to 15 of 15

Thread: How to Show Printer Dialog Box

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    4

    How to Show Printer Dialog Box

    Hi Guys,

    How to show Printer Dialog Box From Visual Basic 6.0

    Users of the program must be able to select the printer where they want the output to be printed.

    Based on the user selection, program has to select the destination printer.

    Please help me on how to handle this requirement

    Rgds
    Nags

  2. #2
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    Re: How to Show Printer Dialog Box

    [QUOTE]um....have you tried the common dialog control?

  3. #3
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Re: How to Show Printer Dialog Box

    Quote Originally Posted by NagsITR
    How to show Printer Dialog Box From Visual Basic 6.0

    Users of the program must be able to select the printer where they want the output to be printed.

    Based on the user selection, program has to select the destination printer.
    The API call you want is PrintDialog. Once they select the printer, Windows will handle routing the document to be printed to the selected printer.

  4. #4
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: How to Show Printer Dialog Box

    VB Code:
    1. 'To show list of printers (not print dialog)
    2. 'Alternative way (without using API):
    3.  
    4. Option Explicit
    5.  
    6. 'Create a new project with a combo box (name: cboPrinter) and two
    7. 'command buttons (name: cmdSelect, Name: cmdPrint)
    8.  
    9. ' Return True if there is a problem.
    10. Private Function SelectPrinter(ByVal printer_name As String) As Boolean
    11.     Dim i As Integer
    12.  
    13.     SelectPrinter = True
    14.     For i = 0 To Printers.Count - 1
    15.         If Printers(i).DeviceName = printer_name Then
    16.             Set Printer = Printers(i)
    17.             SelectPrinter = False
    18.             Exit For
    19.         End If
    20.     Next i
    21. End Function
    22.  
    23. Private Sub cboPrinter_Click()
    24.     cmdSelect.Enabled = (cboPrinter.ListIndex >= 0)
    25. End Sub
    26.  
    27. Private Sub cmdPrint_Click()
    28.     On Error GoTo PrintError
    29.  
    30.     Printer.Line (1 * 1440, 1 * 1440)-(4.35 * 1440, 2 * 1440)
    31.     Printer.CurrentX = 1.25 * 1440
    32.     Printer.CurrentY = 1.25 * 1440
    33.     With Printer.Font
    34.         .Name = "Times New Roman"
    35.         .Size = 24
    36.     End With
    37.     Printer.Print "The quick brown fox jumps over the lazy dog"
    38.     Printer.EndDoc
    39.     MsgBox "Ok"
    40.     Exit Sub
    41.  
    42. PrintError:
    43.     MsgBox "Error " & Format$(Err.Number) & " printing." & _
    44.            vbCrLf & Err.Description
    45.     Exit Sub
    46. End Sub
    47.  
    48. Private Sub cmdSelect_Click()
    49.     If SelectPrinter(cboPrinter.Text) Then
    50.         MsgBox "Printer not found"
    51.     Else
    52.         cmdSelect.Enabled = False
    53.         cmdPrint.Enabled = True
    54.     End If
    55. End Sub
    56.  
    57. Private Sub Form_Load()
    58.     Dim i As Integer
    59.  
    60.     cboPrinter.Clear
    61.     For i = 0 To Printers.Count - 1
    62.         cboPrinter.AddItem Printers(i).DeviceName
    63.     Next i
    64.    
    65.     cmdSelect.Caption = "Select"
    66.     cmdPrint.Caption = "Print"
    67. End Sub
    CS

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    4

    Re: How to Show Printer Dialog Box

    Quote Originally Posted by Fedhax
    The API call you want is PrintDialog. Once they select the printer, Windows will handle routing the document to be printed to the selected printer.

    Hi, Can You can explain the same with a sample VB code.

    I saw this API in API List, however I don't know to handle the same.

    Rgds
    Nags

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    4

    Re: How to Show Printer Dialog Box

    Hi, Iam aware of this code..Thanx

  7. #7
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: How to Show Printer Dialog Box

    I have created a VB project with one command button that displays the Print Dialog.
    Attached Files Attached Files

  8. #8
    New Member
    Join Date
    Feb 2008
    Posts
    2

    Re: How to Show Printer Dialog Box

    Hassan's print dialog project works great. The only question that
    I have on it is how can you tell if the user presses cancel. I didn't
    see the cancel error in your code.

    Thanks

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to Show Printer Dialog Box

    Welcome to the forums.

    What do you need to do is the user presses cancel?

  10. #10
    New Member
    Join Date
    Feb 2008
    Posts
    2

    Re: How to Show Printer Dialog Box

    Thanks Hack,

    I solved my problem by using the new vb print dialog (printdlg.dll) from
    Microsoft. This version will not change the windows default printer which
    was my original problem. This DLL will also return an error code if the user
    clicks the cancel button so that the printing sub is exited before printing
    occurs.

    Here is the link for the new printdlg.dll
    support.microsoft.com/kb/322710/en-us

    Thanks Again!

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to Show Printer Dialog Box

    No problem, and once again, welcome aboard.

  12. #12
    Junior Member
    Join Date
    Apr 2009
    Posts
    16

    Re: How to Show Printer Dialog Box

    Quote Originally Posted by Hassan Basri View Post
    I have created a VB project with one command button that displays the Print Dialog.
    But when we press print button it dose not print any thing

  13. #13
    Member
    Join Date
    Jan 2015
    Posts
    40

    Re: How to Show Printer Dialog Box

    I know this is an old post but it's just what I was looking for and your project has helped me enormously, thank you. But as JamesR, I want the Cancel button to exit the Sub, and this isn't catered for. Can you help?

  14. #14

    Re: How to Show Printer Dialog Box

    PrintDialog printDialog1 = new PrintDialog()
    printDialog1.Document = printDocument1;
    DialogResult result = printDialog1.ShowDialog(this);
    if (result == DialogResult.OK)
    {
    printDocument1.Print();
    }
    Last edited by bestellen; Sep 15th, 2015 at 01:31 PM.

  15. #15
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: How to Show Printer Dialog Box

    Quote Originally Posted by PaMarn View Post
    I know this is an old post but it's just what I was looking for and your project has helped me enormously, thank you. But as JamesR, I want the Cancel button to exit the Sub, and this isn't catered for. Can you help?
    I don't know what your Problem is.
    The PrintDlg-API returns 0 if there is an error processing the call or if the user clicked on cancel.

    RetVal = PrintDlg(PD)

    If RetVal = 0 Then Exit Sub
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width