Option ExplicitPublic Enum PrinterOrientationConstants
    OrientPortrait = 1
    OrientLandscape = 2
End EnumPrivate Type DEVMODE
    dmDeviceName As String * 32
    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 * 32
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
End TypePrivate Type PRINTER_DEFAULTS
    pDataType As String
    pDevMode As Long
    DesiredAccess As Long
End TypePrivate Type PRINTER_INFO_2
    pServerName As Long
    pPrinterName As Long
    pShareName As Long
    pPortName As Long
    pDriverName As Long
    pComment As Long
    pLocation As Long
    pDevMode As Long
    pSepFile As Long
    pPrintProcessor As Long
    pDataType As Long
    pParameters As Long
    pSecurityDescriptor As Long
    Attributes As Long
    Priority As Long
    DefaultPriority As Long
    StartTime As Long
    UntilTime As Long
    Status As Long
    cJobs As Long
    AveragePPM As Long
End Type'*******************'
' iDmpaper值   纸张 '
' 11           A5   '
' 13           B5   '
'*******************'Private Const DM_IN_BUFFER As Long = 8
Private Const DM_OUT_BUFFER As Long = 2
Private Const DM_ORIENTATION As Long = &H1
Private Const DM_PAPERSIZE = &H2&Private Const PRINTER_ACCESS_ADMINISTER As Long = &H4
Private Const PRINTER_ACCESS_USE As Long = &H8
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED _
    Or _
    PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)Private Declare Sub CopyMemory Lib "kernel32" Alias _
    "RtlMoveMemory" _
    (hpvDest As Any, hpvSource As Any, ByVal _
    cbCopy As Long)Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
    "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As _
    Long, pDefault As Any) As LongPrivate Declare Function ClosePrinter Lib "winspool.drv" _
    (ByVal hPrinter As Long) As LongPrivate Declare Function DocumentProperties Lib "winspool.drv" _
    Alias "DocumentPropertiesA" (ByVal hWnd As Long, ByVal hPrinter _
    As Long, _
    ByVal pDeviceName As String, pDevModeOutput As _
    Any, _
    pDevModeInput As Any, _
    ByVal fMode As Long) As LongPrivate Declare Function GetPrinter Lib "winspool.drv" _
    Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
    pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As _
    LongPrivate Declare Function SetPrinter Lib "winspool.drv" _
    Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
    pPrinter As Any, ByVal Command As Long) As Long'在打印或预览之前直接调用SetDefaultPrinterOrientation 打印走向常数。注意:红色注释部分用于改变纸张的大小。
Function SetDefaultPrinterOrientation(ByVal eOrientation As _
    PrinterOrientationConstants, iDmpaper As Integer) As Boolean    Dim bDevMode() As Byte
    Dim bPrinterInfo2() As Byte
    Dim hPrinter As Long
    Dim lResult As Long
    Dim nSize As Long
    Dim sPrnName As String
    Dim dm As DEVMODE
    Dim olddm As DEVMODE
    Dim pd As PRINTER_DEFAULTS
    Dim pi2 As PRINTER_INFO_2    ' 获取默认打印机的设备名称
    sPrnName = Printer.DeviceName
    ' 由于要调用SetPrinter,所以
    ' 如果是在NT下就要求PRINTER_ALL_ACCESS
    pd.DesiredAccess = PRINTER_ALL_ACCESS    ' 获取打印机句柄
    If OpenPrinter(sPrnName, hPrinter, pd) Then
        
        ' 获取PRINTER_INFO_2结构要求的字节数        Call GetPrinter(hPrinter, 2&, 0&, 0&, nSize)
        ReDim bPrinterInfo2(1 To nSize) As Byte
        lResult = GetPrinter(hPrinter, 2, bPrinterInfo2(1), nSize, nSize)
        Call CopyMemory(pi2, bPrinterInfo2(1), Len(pi2))
        nSize = DocumentProperties(0&, hPrinter, sPrnName, 0&, 0&, 0)
              ReDim bDevMode(1 To nSize)
        If pi2.pDevMode Then
            Call CopyMemory(bDevMode(1), ByVal pi2.pDevMode, Len(dm))
        Else
            Call DocumentProperties(0&, hPrinter, sPrnName, bDevMode(1), 0&, DM_OUT_BUFFER)
        End If
        
        Call CopyMemory(dm, bDevMode(1), Len(dm))
        Call CopyMemory(olddm, bDevMode(1), Len(olddm))
        With dm
            ' 设置新的走向
            .dmOrientation = eOrientation
            .dmFields = DM_ORIENTATION
            .dmPaperSize = iDmpaper   '将纸张大小设为iDmpaper,请自行更改所需大小
        '    .dmPaperLength = iDmpaperLength
         '   .dmPaperWidth = iDmpaperWidth
            .dmFields = DM_PAPERSIZE  '必须,否则无法设置纸张大小
        End With
        
        Call CopyMemory(bDevMode(1), dm, Len(dm))        Call DocumentProperties(0&, hPrinter, sPrnName, _
                bDevMode(1), bDevMode(1), DM_IN_BUFFER Or _
                DM_OUT_BUFFER)
        
        pi2.pDevMode = VarPtr(bDevMode(1))
        
        lResult = SetPrinter(hPrinter, 2, pi2, 0&)
        
        Call ClosePrinter(hPrinter)
        SetDefaultPrinterOrientation = True
    Else
        SetDefaultPrinterOrientation = False
    End IfEnd Function

解决方案 »

  1.   

    'Constants used in the DevMode structure
    Private Const CCHDEVICENAME = 32
    Private Const CCHFORMNAME = 32'Constants for NT security
    Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
    Private Const PRINTER_ACCESS_ADMINISTER = &H4
    Private Const PRINTER_ACCESS_USE = &H8
    Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)'Constants used to make changes to the values contained in the DevMode
    Private Const DM_MODIFY = 8
    Private Const DM_IN_BUFFER = DM_MODIFY
    Private Const DM_COPY = 2
    Private Const DM_OUT_BUFFER = DM_COPY
    Private Const DM_DUPLEX = &H1000&
    Private Const DMDUP_SIMPLEX = 1
    Private Const DMDUP_VERTICAL = 2
    Private Const DMDUP_HORIZONTAL = 3
    Private Const DM_ORIENTATION = &H1&
    Private PageDirection As Integer
    '------USER DEFINED TYPES'The DevMode structure contains printing parameters.
    'Note that this only represents the PUBLIC portion of the DevMode.
    '  The full DevMode also contains a variable length PRIVATE section
    '  which varies in length and content between printer drivers.
    'NEVER use this User Defined Type directly with any API call.
    '  Always combine it into a FULL DevMode structure and then send the
    '  full DevMode to the API call.
    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
        dmLogPixels As Integer
        dmBitsPerPel As Long
        dmPelsWidth As Long
        dmPelsHeight As Long
        dmDisplayFlags As Long
        dmDisplayFrequency As Long
        dmICMMethod As Long        ' // Windows 95 only
        dmICMIntent As Long        ' // Windows 95 only
        dmMediaType As Long        ' // Windows 95 only
        dmDitherType As Long       ' // Windows 95 only
        dmReserved1 As Long        ' // Windows 95 only
        dmReserved2 As Long        ' // Windows 95 only
    End TypePrivate Type PRINTER_DEFAULTS
    'Note:
    '  The definition of Printer_Defaults in the VB5 API viewer is incorrect.
    '  Below, pDevMode has been corrected to LONG.
        pDatatype As String
        pDevMode As Long
        DesiredAccess As Long
    End Type
    '------DECLARATIONSPrivate Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
    Private Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal Command As Long) As Long
    Private Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long'The following is an unusual declaration of DocumentProperties:
    '  pDevModeOutput and pDevModeInput are usually declared ByRef.  They are declared
    '  ByVal in this program because we're using a Printer_Info_2 structure.
    '  The pi2 structure contains a variable of type LONG which contains the address
    '  of the DevMode structure (this is called a pointer).  This LONG variable must
    '  be passed ByVal.
    '  Normally this function is called with a BYTE ARRAY which contains the DevMode
    '  structure and the Byte Array is passed ByRef.
    Private Declare Function DocumentProperties Lib "winspool.drv" Alias "DocumentPropertiesA" (ByVal hwnd As Long, ByVal hPrinter As Long, ByVal pDeviceName As String, ByVal pDevModeOutput As Any, ByVal pDevModeInput As Any, ByVal fMode As Long) As LongPrivate Sub SetOrientation(NewSetting As Long, chng As Integer, ByVal frm As Form)
        Dim PrinterHandle As Long
        Dim PrinterName As String
        Dim pd As PRINTER_DEFAULTS
        Dim MyDevMode As DEVMODE
        Dim Result As Long
        Dim Needed As Long
        Dim pFullDevMode As Long
        Dim pi2_buffer() As Long     'This is a block of memory for the Printer_Info_2 structure
            'If you need to use the Printer_Info_2 User Defined Type, the
            '  definition of Printer_Info_2 in the API viewer is incorrect.
            '  pDevMode and pSecurityDescriptor should be defined As Long.
        
        PrinterName = Printer.DeviceName
        If PrinterName = "" Then
            Exit Sub
        End If
        
        pd.pDatatype = vbNullString
        pd.pDevMode = 0&
        'Printer_Access_All is required for NT security
        pd.DesiredAccess = PRINTER_ALL_ACCESS
        
        Result = OpenPrinter(PrinterName, PrinterHandle, pd)
        
        'The first call to GetPrinter gets the size, in bytes, of the buffer needed.
        'This value is divided by 4 since each element of pi2_buffer is a long.
        Result = GetPrinter(PrinterHandle, 2, ByVal 0&, 0, Needed)
        ReDim pi2_buffer((Needed \ 4))
        Result = GetPrinter(PrinterHandle, 2, pi2_buffer(0), Needed, Needed)
        
        'The seventh element of pi2_buffer is a Pointer to a block of memory
        '  which contains the full DevMode (including the PRIVATE portion).
        pFullDevMode = pi2_buffer(7)
        
        'Copy the Public portion of FullDevMode into our DevMode structure
        Call CopyMemory(MyDevMode, ByVal pFullDevMode, Len(MyDevMode))
        
        'Make desired changes
        MyDevMode.dmDuplex = NewSetting
        MyDevMode.dmFields = DM_DUPLEX Or DM_ORIENTATION
        MyDevMode.dmOrientation = chng
        
        'Copy our DevMode structure back into FullDevMode
        Call CopyMemory(ByVal pFullDevMode, MyDevMode, Len(MyDevMode))
        
        'Copy our changes to "the PUBLIC portion of the DevMode" into "the PRIVATE portion of the DevMode"
        Result = DocumentProperties(frm.hwnd, PrinterHandle, PrinterName, ByVal pFullDevMode, ByVal pFullDevMode, DM_IN_BUFFER Or DM_OUT_BUFFER)
        
        'Update the printer's default properties (to verify, go to the Printer folder
        '  and check the properties for the printer)
        Result = SetPrinter(PrinterHandle, 2, pi2_buffer(0), 0&)
        
        Call ClosePrinter(PrinterHandle)
        
        'Note: Once "Set Printer = " is executed, anywhere in the code, after that point
        '      changes made with SetPrinter will ONLY affect the system-wide printer  --
        '      -- the changes will NOT affect the VB printer object.
        '      Therefore, it may be necessary to reset the printer object's parameters to
        '      those chosen in the devmode.
        Dim p As Printer
        For Each p In Printers
            If p.DeviceName = PrinterName Then
                Set Printer = p
                Exit For
            End If
        Next p
        Printer.Duplex = MyDevMode.dmDuplex
    End SubPublic Sub ChngPrinterOrientationLandscape(ByVal frm As Form)
        PageDirection = 2
        Call SetOrientation(DMDUP_SIMPLEX, PageDirection, frm)
    End SubPublic Sub ResetPrinterOrientation(ByVal frm As Form)
     
        If PageDirection = 1 Then
            PageDirection = 2
        Else
            PageDirection = 1
        End If
        Call SetOrientation(DMDUP_SIMPLEX, PageDirection, frm)
    End SubPublic Sub ChngPrinterOrientationPortrait(ByVal frm As Form)    PageDirection = 1
        Call SetOrientation(DMDUP_SIMPLEX, PageDirection, frm)
    End Sub
      

  2.   

    以上两段代码存为两个模块ChngPrinterOrientationLandscape me  '设置为横向