' Declaration for the DeviceCapabilities function API call. Private Declare PtrSafe Function DeviceCapabilities Lib "winspool.drv" _ Alias "DeviceCapabilitiesA" (ByVal lpsDeviceName As String, _ ByVal lpPort As String, ByVal iIndex As Long, lpOutput As Any, _ ByVal lpDevMode As Long) As Long ' DeviceCapabilities function constants. Private Const DC_PAPERNAMES = 16 Private Const DC_PAPERS = 2 Private Const DC_BINNAMES = 12 Private Const DC_BINS = 6 Private Const DEFAULT_VALUES = 0 Sub GetPaperList() Dim lngPaperCount As Long Dim lngCounter As Long Dim hPrinter As Long Dim strDeviceName As String Dim strDevicePort As String Dim strPaperNamesList As String Dim strPaperName As String Dim intLength As Integer Dim strMsg As String Dim aintNumPaper() As Integer On Error GoTo GetPaperList_Err ' Get the name and port of the default printer. Dim printer As String printer = Application.ActivePrinter strDeviceName = Split(printer, " on ")(0) strDevicePort = Split(printer, " on ")(1) 'strDeviceName = Application.ActivePrinter.DeviceName 'strDevicePort = Application.ActivePrinter.Port ' Get the count of paper names supported by the printer. lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _ lpPort:=strDevicePort, _ iIndex:=DC_PAPERNAMES, _ lpOutput:=ByVal vbNullString, _ lpDevMode:=DEFAULT_VALUES) ' Re-dimension the array to the count of paper names. ReDim aintNumPaper(1 To lngPaperCount) ' Pad the variable to accept 64 bytes for each paper name. strPaperNamesList = String(64 * lngPaperCount, 0) ' Get the string buffer of all paper names supported by the printer. lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _ lpPort:=strDevicePort, _ iIndex:=DC_PAPERNAMES, _ lpOutput:=ByVal strPaperNamesList, _ lpDevMode:=DEFAULT_VALUES) ' Get the array of all paper numbers supported by the printer. lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _ lpPort:=strDevicePort, _ iIndex:=DC_PAPERS, _ lpOutput:=aintNumPaper(1), _ lpDevMode:=DEFAULT_VALUES) ' List the available paper names. strMsg = "Papers available for " & strDeviceName & vbCrLf For lngCounter = 1 To lngPaperCount ' Parse a paper name from the string buffer. strPaperName = Mid(String:=strPaperNamesList, _ Start:=64 * (lngCounter - 1) + 1, Length:=64) intLength = VBA.InStr(Start:=1, String1:=strPaperName, String2:=Chr(0)) - 1 strPaperName = Left(String:=strPaperName, Length:=intLength) ' Add a paper number and name to text string for the message box. strMsg = strMsg & vbCrLf & aintNumPaper(lngCounter) _ & vbTab & strPaperName Next lngCounter ' Show the paper names in a message box. MsgBox Prompt:=strMsg GetPaperList_End: Exit Sub GetPaperList_Err: MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _ Title:="Error Number " & Err.Number & " Occurred" Resume GetPaperList_End End Sub