API Printing - Orientation

Posted on 2004-08-31
Last Modified: 2006-11-17
Using the printing api, how do I change the orientation from portrait to landscape for the printing of my document only?
Question by:Clif
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
LVL 22

Expert Comment

ID: 11944114
Hi! What about something like this:

Printer.Orientation = vbPRORLandscape

and you can change it back after printing
LVL 10

Author Comment

ID: 11944162
I can't use the printer object because the printer I am printing to isn't (necessarily) in the user's printer collection.
LVL 22

Accepted Solution

DarkoLord earned 500 total points
ID: 11944311
Try using these APIs: OpenPrinter, GetPrinter, SetPrinter, ClosePrinter

Here are two structures that you need:

   pDatatype As String
   pDevMode As Long
   DesiredAccess As Long
End Type

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 Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
End Type

Good luck!
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 10

Author Comment

ID: 11944377
Can you pupply either some working code or a link to an example of how to use this?
LVL 10

Author Comment

ID: 11944383
Sorry, fat fingers.  :)

Can you supply either some working code or a link to an example of how to use this?

Expert Comment

ID: 11944492
LVL 10

Author Comment

ID: 11944884
It doesn't work.  The link you provided was for VB3.  I did find a 32 bit declaration for Escape, but it still didn't work.
LVL 22

Expert Comment

ID: 11944942
LVL 10

Author Comment

ID: 11945171
To the best that I can figure, the code from the link you provided uses a printer dialog for a lot of the code.

I did find some sample code.  Here is what I have (but doesn't work):

Public Function Orientation(ByVal eOrientation As PrinterOrientationConstants) As Boolean
    Dim bDevMode() As Byte
    Dim bPrinterInfo2() As Byte
    Dim hPrinter As Long
    Dim lResult As Long
    Dim nSize As Long
    Dim dm As DevMode
    Dim pi2 As PRINTER_INFO_2
    ' Get a handle to the printer.
    If OpenPrinterAPI(m_sPrinterName, hPrinter, 0) Then
        ' Get number of bytes requires for PRINTER_INFO_2 structure
        Call GetPrinter(hPrinter, 2&, 0&, 0&, nSize)
        ' Create a buffer of the required size
        ReDim bPrinterInfo2(1 To nSize) As Byte
        ' Fill buffer with structure
        lResult = GetPrinter(hPrinter, 2, bPrinterInfo2(1), nSize, nSize)
        ' Copy fixed portion of structure into VB Type variable
        Call CopyMemory(pi2, bPrinterInfo2(1), Len(pi2))
        ' Get number of bytes requires for DEVMODE structure
        nSize = DocumentProperties(0&, hPrinter, m_sPrinterName, 0&, 0&, 0)
        ' Create a buffer of the required size
        ReDim bDevMode(1 To nSize)
        ' If PRINTER_INFO_2 points to a DEVMODE structure, copy it into our buffer
        If pi2.pDevMode Then
            Call CopyMemory(bDevMode(1), ByVal pi2.pDevMode, Len(dm))
            ' Otherwise, call DocumentProperties to get a DEVMODE structure
            Call DocumentProperties(0&, hPrinter, m_sPrinterName, bDevMode(1), 0&, DM_OUT_BUFFER)
        End If
        ' Copy fixed portion of structure into VB Type variable
        Call CopyMemory(dm, bDevMode(1), Len(dm))
        With dm
            ' Set new orientation
            .dmOrientation = eOrientation
            .dmFields = DM_ORIENTATION
        End With
        ' Copy our Type back into buffer
        Call CopyMemory(bDevMode(1), dm, Len(dm))
        ' Set new orientation
        Call DocumentProperties(0&, hPrinter, m_sPrinterName, bDevMode(1), bDevMode(1), DM_IN_BUFFER Or DM_OUT_BUFFER)
        ' Point PRINTER_INFO_2 at our modified DEVMODE
        pi2.pDevMode = VarPtr(bDevMode(1))
        ' Set new orientation system-wide
        lResult = SetPrinter(hPrinter, 2, pi2, 0&)
        ' Clean up and exit
        Call ClosePrinterAPI(hPrinter)
        Orientation = True
        Orientation = False
    End If
End Function

The way I understand this code, the orientation will remain changed.  This is ok since I can easily set it to landscape, print, then change it back to portrait.

Currently, I create a DC to the printer, then call the above function setting the printer to landscape, TextOut what I need to, call the function again setting the printer to portrait, then destroy the DC I created.

However, like I said, it doesn't seem to be working.  The page(s) still come out in poprtrait.
LVL 10

Author Comment

ID: 11945213
After writing the last post, I decided to try setting the orientation *before* I created the DC.  It works.

Because you led me to the code that worked, you get the points.  Thanks.
LVL 22

Expert Comment

ID: 11945792
Glad you got it to work! Thanks for the points!


Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

728 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