Solved

API Printing - Orientation

Posted on 2004-08-31
11
448 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
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 500 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 …
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…
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…

896 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

17 Experts available now in Live!

Get 1:1 Help Now