This should get you started on iterating the printers collection - it is a little specific to our setup but you should get the idea
This next bit sets the printer you require - if it already in the collection - research the API call AddPrinterConnection to see how to add a printer to the collection.Code:Public Function GetPrinters() '------------------------------ 'Function Description 'Set up printers _ Use Printers collection _ to loop through and add the printer _ to the list in dlgPrinters. 'When this is done then loop the new list _ and identify the current printer and select it _ so then the user sees their currently defaulted _ printer. Dim iList%, obj As Printer, iSLen%, sPrint$, iIndex% 'Loop the printers collection For Each obj In Printers iSLen = InStr(UCase(Printer.DeviceName), "PPMA") sPrint = UCase(obj.DeviceName) dlgPrinters.lstPrint.AddItem UCase(Mid(sPrint, iSLen, 9)) 'Add to the listbox Next 'Loop the list box to find the current printer For iList = 1 To dlgPrinters.lstPrint.ListCount dlgPrinters.lstPrint.ListIndex = iList - 1 If UCase(dlgPrinters.lstPrint.Text) = Mid(UCase(Printer.DeviceName), iSLen, 9) Then iIndex = dlgPrinters.lstPrint.ListIndex 'Bookmark the current printer Exit For End If Next If dlgPrinters.lstPrint.ListCount > 0 Then dlgPrinters.lstPrint.ListIndex = (iIndex) ' Set the current item in the listbox Else Exit Function End If End Function
Again tweaked to suit the setup here but the necessaries are all there.Code:Private Sub cmdSet_Click() Dim obj As Printer, sPrinter$, sNewPrinter$, iInd% sOldPrinter = Printer.DeviceName sPrinter = lstPrint.Text iInd = 0 Dim iLen%, iSLen%, sPrin$ For Each obj In Printers sPrin = obj.DeviceName iSLen = InStr(UCase(sPrin), "PPMA") 'if PPMA present then use this in the list If Mid(UCase(sPrin), iSLen, 9) = UCase(sPrinter) Then 'Debug.Print obj.DeviceName sNewPrinter = Mid(UCase(sPrin), iSLen, 9) Set Printer = obj Exit For GoTo Update End If Next Update: Set obj = Nothing Unload Me End Sub





Reply With Quote