Select Printer From List

AndreRet 1 Tallied Votes 991 Views Share

Let you choose between all printers available on your pc or network. Download zip and enjoy. Royal free.

Option Explicit
Private Declare Function GetProfileString Lib "kernel32.dll" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Private Function GetDefaultPrinter() As String
  Dim strBuffer As String * 254
  Dim iRetValue As Long
  Dim strDefaultPrinterInfo As String
  Dim tblDefaultPrinterInfo() As String
  
  ' Retreive current default printer information
  iRetValue = GetProfileString("windows", "device", ",,,", strBuffer, 254)
  strDefaultPrinterInfo = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
  tblDefaultPrinterInfo = Split(strDefaultPrinterInfo, ",")
  GetDefaultPrinter = tblDefaultPrinterInfo(0)
End Function

Private Sub AddToPrinterList(LBox As ListView, ByRef Counter As Integer, Col1 As String, DefaultPrinter As Boolean)
  'add the printers to the list view control
  'add one to the printer number for the index since the control does not like zero and then
  'remove again when you want to assign the printer
  With LBox
    .ListItems.Add Counter + 1, Col1 & Counter, Col1, 2
    If DefaultPrinter Then
      .ListItems(Counter + 1).Selected = True
    End If
  End With
End Sub

Private Sub cmdCancel_Click()
  Unload Me
End Sub

Private Sub cmdPrint_Click()
  'set the printer and print something
  If Len(PrinterList.SelectedItem.Text) > 0 Then
    Set Printer = Printers(PrinterList.SelectedItem.Index - 1)
    Printer.Copies = NoCopies
  End If
  'add any print statements here that you want
  Printer.Print "hello"
  Printer.EndDoc
  cmdCancel_Click
End Sub

Private Sub Form_Load()
  Dim Counter As Integer
  Dim defPrinter As String
  
  'enumerate the printers collection
  If Printers.Count > 0 Then
    defPrinter = GetDefaultPrinter
    For Counter = 0 To Printers.Count - 1
      AddToPrinterList PrinterList, Counter, Printers(Counter).DeviceName, Printers(Counter).DeviceName = defPrinter
    Next
  Else
    MsgBox "No Printers Are  Installed!" & vbCrLf & vbCrLf & _
     "You must install at least one printer before you can print!", vbCritical, "No Printer Available!"
    Unload Me
  End If
End Sub

Private Sub VScroll1_Change()
  'use a scroll bar to increment the number of copies
  'can add loads of other printing options as you see fit
  NoCopies = Abs(VScroll1)
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.