Solved

API Printing Get/Set Page Size

Posted on 2004-08-31
9
445 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:Clif
  • 5
  • 3
9 Comments
 
LVL 8

Assisted Solution

by:mladenovicz
mladenovicz earned 250 total points
ID: 11941407
0
 
LVL 10

Author Comment

by:Clif
ID: 11941799
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.
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11941997
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 8

Expert Comment

by:mladenovicz
ID: 11942077
0
 
LVL 10

Author Comment

by:Clif
ID: 11942151
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
0
 
LVL 3

Accepted Solution

by:
gajendra earned 250 total points
ID: 11949282
The printer object supports most of the standard paper sizes. You can use the property PaperSize to set and get any of these sizes...

Printer.PaperSize = papersize

Where papersize is one a number from 1 to 41 or 256 each representing a different size (see MSDN help that comes with VB for a complete list).

For Example
   Printer.PaperSize = 4     ' Legal Size -> 8.5" x 14"
you can also use the printer constants like

   Printer.PaperSize = vbPRPSLegal
which is easier to remember.


value 256 may be used if you want to set a custom size.
Printer.PaperSize = 256
Printer.Width = xxx
Printer.Height = yyy

by default xxx and yyy are specified in Twips. For any other units set the ScaleMode property.

Hope this helps

Gajendra
0
 
LVL 10

Author Comment

by:Clif
ID: 11951063
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.
0
 
LVL 10

Author Comment

by:Clif
ID: 11951331
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?
0
 
LVL 10

Author Comment

by:Clif
ID: 12101852
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question