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