Link to home
Start Free TrialLog in
Avatar of Clif
ClifFlag for United States of America

asked on

API Printing Get/Set Page Size

I am printing to a printer through the API.

How do I get and set the page size of the document I will be printing to?

The printed selection will range from a dedicated label printer with 4" x 1 3/4" labels to a standard HP printer with 8 1/2" x 11" or 8 1/2" x 14" paper.

If I am printing to the label printer, there will be, of course, one label per page, with a page feed after each.  If I print to the HP printer, there will be 7 or 9 labels printed on a page before I send a page feed.
SOLUTION
Avatar of mladenovicz
mladenovicz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Clif

ASKER

mladenovicz,
Tried the following code based on the code from the links you provided:

    If OpenPrinter(myPrinter, hPrinter, 0&) Then
        nSize = DocumentProperties(Me.hwnd, hPrinter, myPrinter, 0&, 0&, 0)
        ' Reserve memory for the actual size of the DEVMODE
        ReDim aDevMode(1 To nSize)
       
        ' Fill the DEVMODE from the printer.
        nSize = DocumentProperties(Me.hwnd, hPrinter, myPrinter, aDevMode(1), 0&, DM_OUT_BUFFER)
        ' Copy the predefined portion of the DEVMODE.
        Call CopyMemory(pDevMode, aDevMode(1), Len(pDevMode))
       
        Debug.Print pDevMode.dmDriverVersion  'Shows 24
        Debug.Print pDevMode.dmPaperLength   'Shows 0
        Debug.Print pDevMode.dmPaperWidth     'Shows 0
        ClosePrinter hPrinter
    End If

Basically it's giving 0 for both page width and length.  But it is giving a value for DriverVersion so it is putting values in the structure.
Avatar of mladenovicz
mladenovicz

Private Sub Command4_Click()
Dim myPrinter As String
Dim hPrinter As Long
Dim nSize As Long
Dim pDevMode As DEVMODE
Dim PrinterName As String
Dim aDevMode() As Byte      ' Working DEVMODE

    myPrinter = Printer.DeviceName
    If OpenPrinter(myPrinter, hPrinter, 0&) Then
        nSize = DocumentProperties(Me.hwnd, hPrinter, myPrinter, 0&, 0&, 0)
        ' Reserve memory for the actual size of the DEVMODE
        ReDim aDevMode(1 To nSize)
       
        ' Fill the DEVMODE from the printer.
        nSize = DocumentProperties(Me.hwnd, hPrinter, myPrinter, aDevMode(1), 0&, DM_OUT_BUFFER)
        ' Copy the predefined portion of the DEVMODE.
        Call CopyMemory(pDevMode, aDevMode(1), Len(pDevMode))
       
        Debug.Print pDevMode.dmDriverVersion
        Debug.Print pDevMode.dmPaperLength
        Debug.Print pDevMode.dmPaperWidth
        Debug.Print pDevMode.dmPaperSize
        ClosePrinter hPrinter
    End If
End Sub

gives

 1331
 2794
 2159
 1

for me
Avatar of Clif

ASKER

Your code, cut & paste, changing the device name to "\\myserver\printer1"

gives

 24
 0
 0
 1

for me

Your second link is basically what I already had and still returns 0s
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Clif

ASKER

gajendra,
What paper size is represented by the 4" x 1 3/4" labels as I noted above?

Aside from that, though, I cannot use the printer object.  The printer I will be printing to may no be part of the printer collection.
Avatar of Clif

ASKER

I found out how to get the page size.  I'm not sure what the unit of measure is, though.


Private Declare Function DeviceCapabilities Lib "winspool.drv" Alias "DeviceCapabilitiesA" _
    (ByVal lpDeviceName As String, _
     ByVal lpPort As String, _
     ByVal iIndex As Long, _
     lpOutput As Any, _
     lpDevMode As Any) As Long
Private Declare Function DeviceCapabilities_pt Lib "winspool.drv" Alias "DeviceCapabilitiesA" _
    (ByVal lpDeviceName As String, _
     ByVal lpPort As String, _
     ByVal iIndex As Long, _
     lpOutput As Any, _
     lpDevMode As Any) As Points
Private Const DC_MAXEXTENT = 5
Private Type Points
        x  As Integer
        y  As Integer
End Type

Private Sub Command1_Click()
    Dim nPPIx As Integer
    Dim nPPIy As Integer
    Dim myPrinter As String
    myPrinter = "\\mynetwork2\printer2"
   
    Dim pt As Points
    result = DeviceCapabilities(myPrinter, vbNull, DC_MAXEXTENT, ByVal 0&, ByVal 0&)
    pt = DeviceCapabilities_pt(myPrinter, vbNull, DC_MAXEXTENT, ByVal 0&, ByVal 0&)
    nPPIx = pt.x / 8.5
    nPPIy = pt.y / 11
End Sub

Any ideas on how to convert the returned values to pixels or, better yet, inches?
Avatar of Clif

ASKER

I decided to use the printer object after all.  I found out how to add a printer (and remove a printer) "on the fly".

Thanks.