Solved

API Printing Get/Set Page Size

Posted on 2004-08-31
9
443 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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11942077
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now