Tipp 5.22 - Papiergrößen ermitteln

Wie kann ich die verfügbare Papiergrößen eines Druckers ermitteln?

Mit dieser Funktion können Sie die verfügbare Papiergrößen eines Druckers ermitteln. Kopieren Sie einfach nachfolgenden Quellcode in die Zwischenablage und fügen Sie anschließend den Inhalt der Zwischenablage in ein neues Modul ein. Die Aufrufparameter finden Sie im Quellcode beschrieben.

Api-AufrufeVerwendete Win32-Api-Aufrufe und Typen: OpenPrinter, DeviceCapabilities, ClosePrinter

Beispiel:

Option Explicit
Public Declare Function OpenPrinter _
    Lib "winspool.drv" Alias _
    "OpenPrinterA" _
    (ByVal pPrinterName As String, _
    phPrinter As Long, _
    ByVal pDefault As Long) As Long
Public Declare Function DeviceCapabilities _
    Lib "winspool.drv" Alias _
    "DeviceCapabilitiesA" _
    (ByVal lpsDeviceName As String, _
    ByVal lpPort As String, _
    ByVal iIndex As Long, _
    lpOutput As Any, _
    ByVal dev As Long) As Long
Public Declare Function ClosePrinter _
    Lib "winspool.drv" _
    (ByVal hPrinter As Long) As Long
 
Function GetPrinterFormList( _
         asPrntFrms() As String, _
         Optional sDeviceName As String = vbNullString) _
         As Long
  '// -----------------------------------------------------
  '// Methode:   | Verfügbare Papiergrößen eines Druckers
  '//            | ermitteln
  '// -----------------------------------------------------
  '// Parameter: | asPrntFrms -Array für die Größenangaben
  '//            | sDeviceName -Drucker, falls nicht Stand.
  '// -----------------------------------------------------
  '// Rückgabe:  | s.o.
  '// -----------------------------------------------------
  '// Autor:     | Stefan Kulpa
  '//            | EDV Innovation & Consulting - Dormagen
  '// -----------------------------------------------------
  '// Beispiel:
  '// -----------------------------------------------------
  '// Dim asPrnt() As String
  '// Dim l As Long, i As Long
  '// l = GetPrinterFormList(asPrnt())
  '// For i = 0 To l - 1
  '//     Debug.Print asPrnt(i)
  '// Next
  '// -----------------------------------------------------
  Const DC_PAPERNAMES As Long = 16
  Dim lPaperCount     As Long
  Dim lcounter        As Long
  Dim lPrinter        As Long
  Dim sDevicePort     As String
  Dim sPaperNamesList As String
  Dim sNextString     As String
 
  If Len(sDeviceName) = 0 Then _
      sDeviceName = Printer.DeviceName
  If OpenPrinter(sDeviceName, lPrinter, 0) <> 0 Then
    lPaperCount = DeviceCapabilities(sDeviceName, _
        sDevicePort, _
        DC_PAPERNAMES, _
        ByVal vbNullString, _
        0)
    ReDim asPrntFrms(lPaperCount)
    sPaperNamesList = String(64 * lPaperCount, 0)
    lPaperCount = DeviceCapabilities(sDeviceName, _
        sDevicePort, _
        DC_PAPERNAMES, _
        ByVal sPaperNamesList, _
        0)
    For lcounter = 1 To lPaperCount
      sNextString = _
          Mid(sPaperNamesList, 64 * (lcounter - 1) + 1, 64)
      sNextString = _
          Left(sNextString, InStr(1, _
          sNextString, Chr(0)) - 1)
      asPrntFrms(lcounter - 1) = sNextString
    Next lcounter
    ClosePrinter (lPrinter)
    GetPrinterFormList = lPaperCount
  End If
 
End Function