1

I am facing an issue in VB 6 while creating a Printer Object. Basically, I need to create a printer object so that I can set the correct tray on which printing needs to be performed.

I have the Printer Name with me.

All the code that I am able to find online involves looping through all the available printers and finding a match with our printer name.

Is there a way I can create the Printer object prn directly from the Printer name.

Any help would be appreciated.

Pankaj Jaju
  • 5,371
  • 2
  • 25
  • 41
varuog
  • 93
  • 2
  • 10

1 Answers1

2

You can't. The VB6 Printers collection is accessed only by index, not by name. See Visual Studio 6 Printer Object, Printers Collection.

So you have to search the collection for the printer you want. For instance:

Private Function FindPrinter(PrinterName As String) As Printer
  Dim i As Integer
  For i = 0 To Printers.Count - 1
    If Printers(i).DeviceName = PrinterName Then
      Set FindPrinter = Printers(i)
      Exit For
    End If
  Next i
  Exit Function
End Function

The above doesn't handle the situation where the printer you're looking for isn't in the collection. You'll want to add logic to cover that condition - what you'll want to do is specific to your particular tasks and requirements. This example is also a case-sensitive name search, so keep that in mind, too.

MarkL
  • 1,721
  • 1
  • 11
  • 22
  • Thanks for the reply. The code mentioned by you is what I am currently using in my application. However, when there are a large number of printers (2500+) this tends to get very slow. So was looking for a workaround. – varuog Nov 17 '16 at 05:26
  • Unless you are switching printers, you don't need to search the Printers collection every time, of course. Search it on app startup (or user selection, etc), then retain that reference to the Printer object you are using. Little concern then about performance on searching the Printers collection. – MarkL Nov 17 '16 at 15:14