?
Solved

API Printing - Orientation

Posted on 2004-08-31
11
Medium Priority
?
483 Views
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?
0
Comment
Question by:Clif
[X]
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
11 Comments
 
LVL 22

Expert Comment

by:DarkoLord
ID: 11944114
Hi! What about something like this:

Printer.Orientation = vbPRORLandscape

and you can change it back after printing
0
 
LVL 10

Author Comment

by:Clif
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.
0
 
LVL 22

Accepted Solution

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

Here are two structures that you need:

Private Type PRINTER_DEFAULTS
   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!
Darko
0
Technology Partners: 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

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

Author Comment

by:Clif
ID: 11944383
Sorry, fat fingers.  :)

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

Expert Comment

by:mladenovicz
ID: 11944492
0
 
LVL 10

Author Comment

by:Clif
ID: 11944884
mladenovicz,
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.
0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 11944942
0
 
LVL 10

Author Comment

by:Clif
ID: 11945171
DarkoLord,
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))
        Else
            ' 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
    Else
        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.
0
 
LVL 10

Author Comment

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

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

Expert Comment

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

Darko
0

Featured Post

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!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 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