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
Re: How to Show Printer Dialog Box
[QUOTE]um....have you tried the common dialog control?
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.
Re: How to Show Printer Dialog Box
VB Code:
'To show list of printers (not print dialog)
'Alternative way (without using API):
Option Explicit
'Create a new project with a combo box (name: cboPrinter) and two
'command buttons (name: cmdSelect, Name: cmdPrint)
' Return True if there is a problem.
Private Function SelectPrinter(ByVal printer_name As String) As Boolean
Dim i As Integer
SelectPrinter = True
For i = 0 To Printers.Count - 1
If Printers(i).DeviceName = printer_name Then
Set Printer = Printers(i)
SelectPrinter = False
Exit For
End If
Next i
End Function
Private Sub cboPrinter_Click()
cmdSelect.Enabled = (cboPrinter.ListIndex >= 0)
End Sub
Private Sub cmdPrint_Click()
On Error GoTo PrintError
Printer.Line (1 * 1440, 1 * 1440)-(4.35 * 1440, 2 * 1440)
Printer.CurrentX = 1.25 * 1440
Printer.CurrentY = 1.25 * 1440
With Printer.Font
.Name = "Times New Roman"
.Size = 24
End With
Printer.Print "The quick brown fox jumps over the lazy dog"
Printer.EndDoc
MsgBox "Ok"
Exit Sub
PrintError:
MsgBox "Error " & Format$(Err.Number) & " printing." & _
vbCrLf & Err.Description
Exit Sub
End Sub
Private Sub cmdSelect_Click()
If SelectPrinter(cboPrinter.Text) Then
MsgBox "Printer not found"
Else
cmdSelect.Enabled = False
cmdPrint.Enabled = True
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
cboPrinter.Clear
For i = 0 To Printers.Count - 1
cboPrinter.AddItem Printers(i).DeviceName
Next i
cmdSelect.Caption = "Select"
cmdPrint.Caption = "Print"
End Sub
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
Re: How to Show Printer Dialog Box
Hi, Iam aware of this code..Thanx
1 Attachment(s)
Re: How to Show Printer Dialog Box
I have created a VB project with one command button that displays the Print Dialog.
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
Re: How to Show Printer Dialog Box
Welcome to the forums. :wave:
What do you need to do is the user presses cancel?
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!
Re: How to Show Printer Dialog Box
No problem, and once again, welcome aboard. :)
Re: How to Show Printer Dialog Box
Quote:
Originally Posted by
Hassan Basri
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
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?
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();
}
Re: How to Show Printer Dialog Box
Quote:
Originally Posted by
PaMarn
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