Solved

API Printing - Orientation

Posted on 2004-08-31
11
443 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
  • 6
  • 4
11 Comments
 
LVL 22

Expert Comment

by:DarkoLord
Comment Utility
Hi! What about something like this:

Printer.Orientation = vbPRORLandscape

and you can change it back after printing
0
 
LVL 10

Author Comment

by:Clif
Comment Utility
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 500 total points
Comment Utility
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
 
LVL 10

Author Comment

by:Clif
Comment Utility
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
Comment Utility
Sorry, fat fingers.  :)

Can you supply either some working code or a link to an example of how to use this?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:mladenovicz
Comment Utility
0
 
LVL 10

Author Comment

by:Clif
Comment Utility
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
Comment Utility
0
 
LVL 10

Author Comment

by:Clif
Comment Utility
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
Comment Utility
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
Comment Utility
Glad you got it to work! Thanks for the points!

Darko
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

744 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

16 Experts available now in Live!

Get 1:1 Help Now