[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


API Printing - Orientation

Posted on 2004-08-31
Medium Priority
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 2000 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!
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Suggested Courses

650 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