HOWTO: Determine Available PaperBins With DeviceCapabilities API

ID: Q194789


The information in this article applies to:


SUMMARY

If setting the PaperBin property of the Printer object fails to select the desired bin, it may be because the printer driver doesn't recognize the value being assigned. For example, you assign the constant for the upper bin (vbPRBNUpper) and it fails. It turns out that the printer driver considers its upper bin to be the Cassette bin. In this case, you need to use the Cassette constant to select the printer's upper bin.

This article shows how to enumerate a printer's paper bins.


MORE INFORMATION

Step-by-Step Example

  1. Create a Standard EXE project in Visual Basic. Form1 is created by default.


  2. Add a CommandButton and TextBox to the form.


  3. Set Text1's ScrollBar property to "2 - Vertical" and the MultiLine property to "True."


  4. Place the following code into Form1's code window:
    
          Option Explicit
    
          Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
             "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
             ByVal pDefault As Long) As Long
          Private Declare Function ClosePrinter Lib "winspool.drv" ( _
             ByVal hPrinter As Long) As Long
          Private Declare Function DeviceCapabilities Lib "winspool.drv" _
             Alias "DeviceCapabilitiesA" (ByVal lpDeviceName As String, _
             ByVal lpPort As String, ByVal iIndex As Long, lpOutput As Any, _
             ByVal dev As Long) As Long
    
          Private Const DC_BINS = 6
          Private Const DC_BINNAMES = 12
    
          Private Sub Command1_Click()
             Dim prn As Printer
             Dim hPrinter As Long
             Dim dwbins As Long
             Dim ct As Long
             Dim nameslist As String
             Dim nextString As String
             Dim numBin() As Integer
    
             Text1.Font.Name = "Courier New"
             Text1.Font.Size = 12
             Text1.Text = ""
             For Each prn In Printers
                If OpenPrinter(prn.DeviceName, hPrinter, 0) <> 0 Then
                   dwbins = DeviceCapabilities(prn.DeviceName, prn.Port, _
                     DC_BINS, ByVal vbNullString, 0)
                   ReDim numBin(1 To dwbins)
                   nameslist = String(24 * dwbins, 0)
                   dwbins = DeviceCapabilities(prn.DeviceName, prn.Port, _
                     DC_BINS, numBin(1), 0)
                   dwbins = DeviceCapabilities(prn.DeviceName, prn.Port, _
                     DC_BINNAMES, ByVal nameslist, 0)
                   If Text1.Text <> "" Then
                      Text1.Text = Text1.Text & vbCrLf & vbCrLf
                   End If
                   Text1.Text = Text1.Text & prn.DeviceName
                   For ct = 1 To dwbins
                      nextString = Mid(nameslist, 24 * (ct - 1) + 1, 24)
                      nextString = Left(nextString, InStr(1, nextString, _
                        Chr(0)) - 1)
                      nextString = String(6 - Len(CStr(numBin(ct))), " ") & _
                      numBin(ct) & "  " & nextString
                      Text1.Text = Text1.Text & vbCrLf & nextString
                   Next ct
                   Call ClosePrinter(hPrinter)
                Else   ' OpenPrinter failed, so cannot retrieve information
                   Text1.Text = Text1.Text & vbCrLf & vbCrLf & prn.DeviceName _
                     & vbCrLf & "  <Unavailable>"
                End If
             Next prn
          End Sub
    
          Private Sub Form_Load()
             ' Size and position the Form and controls
             Me.Height = 7000
             Me.Width = 7000
             Text1.Top = 100
             Text1.Left = 100
             Text1.Height = 6450
             Text1.Width = 5000
             Text1.Text = ""   ' Clear the TextBox
             Command1.Left = 5300
             Command1.Top = 1000
             Command1.Width = 1500
             Command1.Caption = "List Bins"
          End Sub
     


  5. Run the project and click on the CommandButton labeled "List Bins." The TextBox will be filled with a list of all installed printers and the PaperBin settings each one supports.

    NOTE: This approach lists all PaperBins known to the printer driver. Every bin listed is not guaranteed to be physically installed on the printer. For example, some printers are available with an optional envelope feeder. This selection will be reported by the API even if that feature isn't currently installed on the printer.



Keywords          : kbAPI kbPrinting kbVBp kbVBp400 kbVBp500 kbVBp600 
Version           : WINDOWS:4.0,5.0,6.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: May 24, 1999