Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printer Object 2

Status
Not open for further replies.

zemp

Programmer
Jan 27, 2002
3,301
CA
Is there any way to get the list of available paper sizes for a specific printer via the Printer object or any another object or method?

Thanks and Good Luck!

zemp
 


Hi Zemp:

Haven't tried this, but will suggest it.

Select each possible PaperBin then read the PaperSize back from the printer.

Cassie
 
No that doesn't work. The .PaperBin refers to a specific paper tray from which the printer can draw different paper sizes. Thanks for the input.

Any other suggestions.

Thanks and Good Luck!

zemp
 

zemp,

Here is a quick modification of some code I use to get the printer bins.

You will need a form with a combobox on it and the following code (form1, combo1)...
[tt]
Option Explicit

Private Sub Form_Load()

Dim P As Printer

For Each P In Printers
Combo1.AddItem P.DeviceName
Next P

End Sub

Private Sub Combo1_Click()
Call GetPaperNames(Combo1.Text)
End Sub
[/tt]

then in a module...
[tt]
Option Explicit

Private Const DEFAULT_VALUES = 0

Private Const DC_PAPERNAMES = 16
Private Const DC_PAPERS = 2
Private Const DC_PAPERSIZE = 3

Private Type DEVMODE
dmDeviceName As String * CCHDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCHFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Long
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type

Private Type PRINTER_DEFAULTS
pDatatype As String
pDevMode As DEVMODE
DesiredAccess As Long
End Type

Public Type POINTAPI
X As Long
Y As Long
End Type

Public Function GetPaperNames(strPrinterName As String) As String
' Uses the DeviceCapabilities API function to display a
' message with the name of the default printer and a
' list of the paper it supports.

Dim lngPaperCount As Long
Dim lngCounter As Long
Dim strDeviceName As String
Dim strDevicePort As String
Dim strPaperNameList As String
Dim strPaperName As String
Dim intLength As Integer
Dim strMsg As String
Dim intPaperNumber() As Integer
Dim P As Printer
Dim Pnt() As POINTAPI
Dim strTemp As String, intTemp As Integer

For Each P In Printers
If P.DeviceName = strPrinterName Then Exit For
Next P

On Error GoTo GetPaperNames_Err

' Get name and port of the default printer.
strDeviceName = P.DeviceName
strDevicePort = P.Port

' Get count of paper bin names supported by printer.
lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, lpPort:=strDevicePort, iIndex:=DC_PAPERNAMES, lpOutput:=ByVal vbNullString, lpDevMode:=DEFAULT_VALUES)

' Re-dimension arrays to count of paper names.
ReDim intPaperNumber(1 To lngPaperCount)
ReDim Pnt(1 To lngPaperCount)

'Pad variable to accept 64 bytes for each paper name.
strPaperNameList = String(Number:=64 * lngPaperCount, Character:=0)

'Get string buffer of paper names supported by printer.
lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, lpPort:=strDevicePort, iIndex:=DC_PAPERNAMES, lpOutput:=ByVal strPaperNameList, lpDevMode:=DEFAULT_VALUES)

'Get array of paper numbers supported by printer
lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, lpPort:=strDevicePort, iIndex:=DC_PAPERS, lpOutput:=intPaperNumber(1), lpDevMode:=0)

'get point list of paper sizes for each paper
lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, lpPort:=strDevicePort, iIndex:=DC_PAPERSIZE, lpOutput:=Pnt(1), lpDevMode:=0)

'List available paper names.
strMsg = "Paper available for " & strDeviceName & vbCrLf
For lngCounter = 1 To lngPaperCount

' Parse a paper name from string buffer.
strPaperName = Mid(String:=strPaperNameList, Start:=64 * (lngCounter - 1) + 1, Length:=64)
intLength = VBA.InStr(Start:=1, String1:=strPaperName, String2:=Chr(0)) - 1
strPaperName = Left(String:=strPaperName, Length:=intLength)

strTemp = intPaperNumber(lngCounter) & vbTab & strPaperName
intTemp = Len(strTemp)
strTemp = strTemp & String(40 - intTemp, " ")

' Add bin name and number to text string for message box.
strMsg = strMsg & vbCrLf & strTemp & vbTab & "X=" & Pnt(lngCounter).X & vbTab & "Y=" & Pnt(lngCounter).Y

Next lngCounter

' Show paper bin numbers and names in message box.
'MsgBox Prompt:=strMsg

Debug.Print strMsg

'Printer.Print formMain.Combo1.Text
'Printer.Print strMsg
'Printer.EndDoc

GetPaperNames_End:
Exit Function
GetPaperNames_Err:
MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
Title:="Error Number " & Err.Number & " Occurred"
Resume GetPaperNames_End
End Function
[/tt]

I think that is what you are asking for. (and I think I got it working right on win2kSP2 VB6SP5).

There is a whole lot more info you can get with the DeviceCapabilities API.

Good Luck


 
Thanks vb5, I have a couple of questions/requests.

First in your UDT 'DEVMODE' you have two constants that are not defined 'CCHDEVICENAME' and 'CCHFORMNAME'. Can you post thier values? I am not sure if the code will work properly if I comment them out.

Second you call a function called 'DeviceCapabilities' in the code. Could you possibly post that function as well? Or give me information on where to find it.

Thanks and Good Luck!

zemp
 

Ahh damn! it figures I would miss a couple of things like the API itself!!!

[tt]
Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32

Private Declare 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

[/tt]

Please note that the DeviceCapabilities API that I am using here has been modified from its origional form which follows...
[tt]
Public Declare Function DeviceCapabilities Lib "winspool.drv" Alias "DeviceCapabilitiesA" (ByVal lpDeviceName As String, ByVal lpPort As String, ByVal iIndex As Long, ByVal lpOutput As String, lpDevMode As DEVMODE) As Long
[/tt]

Good Luck

 
Works like a charm! Thank You very much. Deserves a star.

BTW, I used the first (altered) API declaration.

Thanks and Good Luck!

zemp
 

Your welcome and yes that was the one to use.

Have fun with it :)

Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top