Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printer --> changing tray, copies, quality, and default printer

Status
Not open for further replies.

devilman0

IS-IT--Management
Joined
Nov 14, 2002
Messages
257
Location
US
I want to change copies, quality, and default printer using the setprinter api, devmode, etc. I can change the orentation with some sample code from microsoft, however, i can't figure out how to do any of the above, seems impossable to me. The reason printer.whatever doesn't work is because i use internet explorer to create a custom report then have it print silently. I wan't to set the default properites for the printer i wish to use. I have a commondialog that recieves all the required info about the printer so getting device specific info isn't important. I store this info in an ini file to retreave later, (printer, driver name, port, orentation, tray, quality, copies, etc.) so that hard part is done.
Thanks in advance. Thanks,
James
[shadeshappy] [evil]
 

Yes, it is easy to set the default printer since you are using the common dialog (CD) (printerdefault = true). Or search this site for SetDefaultPrinter. This is an API. You may have to set you parameters to "Any Date" to find the reference for it. For copies you can once again recieve this information from the CD and for each copy you will have to send a print job.

Now for the quality that will take some doing but from reading your post you are on the right track.

Good Luck

 
I realy don't want to use the common dialog every time i print. I have it right now you do a setup while actually setting up the program, and there is also an option to set it up later too, and this is a slightly modified version of the cd to work better with nt (namely 2k and xp). My software uses an intenet expoler ocx on a form for displaying a preview of the page (it is sized to match the size of the paper exactialy minus the margins). I have two options, prevew and print, when i press print it doesn't show the preview but it does load the form, activates the print command for internet exploer in hidden mode, so all the default settings for the printer are used for printing. You can see i decided to do this the hard way. Some have said to use crystal reports, but seeing how this software is free, i didn't see paying out money for a report engine, there is also the data reports that come with vb6, but they seem to me (after much experimenting) that they aren't very flexable. I can further elaborate on how i did this with ie, that that is another thread for a diff time.

-In short, commondialog is out, i have stored variables that'll have all the options setup already (so it will be correct with the driver for the printer) Thanks,
James
[shadeshappy] [evil]
 
You could have a look at my ActiveX DLL, PDXPro
It uses only API functions to manage printer settings. The trial version includes a test bench exe with full source code so you can test its functionality without writing code yourself.

Paul Bent
Northwind IT Systems
 
The control is nice, but the problem is the $149 price tag. It is well above the scope of my free project. I have no problem writing my own code to do that, and if need be, can figure out some c code if it was the only way to use those apis, but i don't think that is the case, i can set the orentation for it, but i can't get it to set copies, paperbin. so i am stuck on that. If someone needs me to post/email code i sure can. Thanks,
James
[shadeshappy] [evil]
 
here is my code:
Code:
Option Explicit
Private PageDirection As Integer
Private OldPrinterName As String
Private OldPrinterDriverName As String
Private OldPrinterQuality As Long
Private OldPrinterQuantaty As Long
Private OldPrinterOrentation As Long
Private OldPrinterTray As Long
Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32
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)
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&
Public Const DMDUP_SIMPLEX = 1
Private Const DMDUP_VERTICAL = 2
Private Const DMDUP_HORIZONTAL = 3
Private Const DM_ORIENTATION = &H1&
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 Type
Private Type PRINTER_DEFAULTS
    pDatatype As String
    pDevMode As Long
    DesiredAccess As Long
End Type
Private 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
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 Long

Public Sub ChngOrientationLandscape()
PageDirection = 2
 Call SetOrientation(DMDUP_SIMPLEX, PageDirection)
End Sub
Public Sub ReSetOrientation()
 
If PageDirection = 1 Then
 PageDirection = 2
Else
 PageDirection = 1
End If
Call SetOrientation(DMDUP_SIMPLEX, PageDirection)
End Sub
Public Sub ChngOrientationPortrait()
PageDirection = 1
Call SetOrientation(DMDUP_SIMPLEX, 1)
End Sub


Public Sub SetOrient(ByVal Landscape As Boolean)
Select Case Landscape
Case True
ChngOrientationLandscape
Case False
ReSetOrientation
End Select
End Sub


Public Sub SetOrientation(NewSetting As Long, chng As Integer)
    On Error GoTo 10
    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
    PrinterName = Printer.DeviceName
    If PrinterName = "" Then
        Exit Sub
    End If
    
    pd.pDatatype = vbNullString
    pd.pDevMode = 0&
    pd.DesiredAccess = PRINTER_ALL_ACCESS
    Result = OpenPrinter(PrinterName, PrinterHandle, pd)
    Result = GetPrinter(PrinterHandle, 2, ByVal 0&, 0, Needed)
    ReDim pi2_buffer((Needed \ 4))
    Result = GetPrinter(PrinterHandle, 2, pi2_buffer(0), Needed, Needed)
    
    pFullDevMode = pi2_buffer(7)
    
    Call CopyMemory(MyDevMode, ByVal pFullDevMode, Len(MyDevMode))
    
    MyDevMode.dmDuplex = NewSetting
    MyDevMode.dmFields = DM_DUPLEX Or DM_ORIENTATION
    MyDevMode.dmOrientation = chng
    
    
    Call CopyMemory(ByVal pFullDevMode, MyDevMode, Len(MyDevMode))
    
    Result = DocumentProperties(Form1.hwnd, PrinterHandle, PrinterName, ByVal pFullDevMode, ByVal pFullDevMode, DM_IN_BUFFER Or DM_OUT_BUFFER)
    
    Result = SetPrinter(PrinterHandle, 2, pi2_buffer(0), 0&)
    
    Call ClosePrinter(PrinterHandle)
    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
    Exit Sub
10
    MsgBox Err.Number & " " & Err.Description
End Sub

Public Sub SetPrinterJob(PrinterName As String, PrinterQuality As Long, PrinterQuantaty As Long, PrinterOrentation As Long, PrinterTray As Long)
' On Error GoTo 10
    Dim PrinterHandle As Long
    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
    Dim i As Long
    
    If PrinterName = "" Then
        Exit Sub
    End If
    OldPrinterName = Printer.DeviceName
    pd.pDatatype = vbNullString
    pd.pDevMode = 0&
    pd.DesiredAccess = PRINTER_ALL_ACCESS
    Result = OpenPrinter(PrinterName, PrinterHandle, pd)
    Result = GetPrinter(PrinterHandle, 2, ByVal 0&, 0, Needed)
    ReDim pi2_buffer((Needed \ 4))
    Result = GetPrinter(PrinterHandle, 2, pi2_buffer(0), Needed, Needed)
    pFullDevMode = pi2_buffer(7)
    
    Call CopyMemory(MyDevMode, ByVal pFullDevMode, Len(MyDevMode))
    'remember old settings to set back later...
    OldPrinterTray = Printer.PaperBin
    OldPrinterOrentation = Printer.Orientation
    OldPrinterQuality = Printer.PrintQuality
    OldPrinterQuantaty = Printer.Copies
    'here i set the info
    MyDevMode.dmDeviceName = Printer.DeviceName
    MyDevMode.dmSize = Len(MyDevMode)
    MyDevMode.dmFields = DM_DUPLEX Or DM_ORIENTATION
    MyDevMode.dmOrientation = PrinterOrentation
    MyDevMode.dmDefaultSource = PrinterTray
    MyDevMode.dmPrintQuality = PrinterQuality
    MyDevMode.dmCopies = CLng(PrinterQuantaty)
    MyDevMode.dmDuplex = DMDUP_SIMPLEX
    'small sleep timer
    For i = 0 To 255
    DoEvents
    Next i
    'here it should be setting the info that i changed above
    Call CopyMemory(ByVal pFullDevMode, MyDevMode, Len(MyDevMode))
    'not sure what this does, but doesn't work without this.
    Result = DocumentProperties(Form1.hwnd, PrinterHandle, PrinterName, ByVal pFullDevMode, ByVal pFullDevMode, DM_IN_BUFFER Or DM_OUT_BUFFER)
    Result = SetPrinter(PrinterHandle, 2, pi2_buffer(0), 0&)
    
    Call ClosePrinter(PrinterHandle)
    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
    Exit Sub
10
    MsgBox Err.Number & " " & Err.Description


End Sub
Public Sub ReSetPrinterJob()
 On Error Resume Next
    Dim PrinterHandle As Long
    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
    Dim PrinterName As String
    PrinterName = OldPrinterName
    pd.pDatatype = vbNullString
    pd.pDevMode = 0&
    pd.DesiredAccess = PRINTER_ALL_ACCESS
    Result = OpenPrinter(PrinterName, PrinterHandle, pd)
    Result = GetPrinter(PrinterHandle, 2, ByVal 0&, 0, Needed)
    ReDim pi2_buffer((Needed \ 4))
    Result = GetPrinter(PrinterHandle, 2, pi2_buffer(0), Needed, Needed)
    
    pFullDevMode = pi2_buffer(7)
    
    Call CopyMemory(MyDevMode, ByVal pFullDevMode, Len(MyDevMode))
    
    MyDevMode.dmDuplex = DMDUP_SIMPLEX
    MyDevMode.dmFields = DM_DUPLEX Or DM_ORIENTATION
    MyDevMode.dmOrientation = OldPrinterOrentation
    MyDevMode.dmDefaultSource = OldPrinterTray
    MyDevMode.dmPrintQuality = OldPrinterQuality
    MyDevMode.dmCopies = OldPrinterQuantaty
    
    Call CopyMemory(ByVal pFullDevMode, MyDevMode, Len(MyDevMode))
    Result = DocumentProperties(JobInformation.hwnd, PrinterHandle, PrinterName, ByVal pFullDevMode, ByVal pFullDevMode, DM_IN_BUFFER Or DM_OUT_BUFFER)
    
    Result = SetPrinter(PrinterHandle, 2, pi2_buffer(0), 0&)
    
    Call ClosePrinter(PrinterHandle)
    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
    Exit Sub
10
    MsgBox Err.Number & " " & Err.Description


End Sub

Thanks,
James
[shadeshappy] [evil]
 
Ah ha, i figured it out. For those who want to use the api, this is how it works for me. I removed two lines:
mydevmode.dmsize = len(mydevmode)
MyDevMode.dmFields = DM_DUPLEX Or DM_ORIENTATION
here it is:
Code:
Option Explicit
Private PageDirection As Integer
Private OldPrinterName As String
Private OldPrinterDriverName As String
Private OldPrinterQuality As Long
Private OldPrinterQuantaty As Long
Private OldPrinterOrentation As Long
Private OldPrinterTray As Long
Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32
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)
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&
Public Const DMDUP_SIMPLEX = 1
Private Const DMDUP_VERTICAL = 2
Private Const DMDUP_HORIZONTAL = 3
Private Const DM_ORIENTATION = &H1&
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 Type
Private Type PRINTER_DEFAULTS
    pDatatype As String
    pDevMode As Long
    DesiredAccess As Long
End Type
Private 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
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 Long

Public Sub ChngOrientationLandscape()
PageDirection = 2
 Call SetOrientation(DMDUP_SIMPLEX, PageDirection)
End Sub
Public Sub ReSetOrientation()
 
If PageDirection = 1 Then
 PageDirection = 2
Else
 PageDirection = 1
End If
Call SetOrientation(DMDUP_SIMPLEX, PageDirection)
End Sub
Public Sub ChngOrientationPortrait()
PageDirection = 1
Call SetOrientation(DMDUP_SIMPLEX, 1)
End Sub


Public Sub SetOrient(ByVal Landscape As Boolean)
Select Case Landscape
Case True
ChngOrientationLandscape
Case False
ReSetOrientation
End Select
End Sub


Public Sub SetOrientation(NewSetting As Long, chng As Integer)
    On Error GoTo 10
    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
    PrinterName = Printer.DeviceName
    If PrinterName = "" Then
        Exit Sub
    End If
    
    pd.pDatatype = vbNullString
    pd.pDevMode = 0&
    pd.DesiredAccess = PRINTER_ALL_ACCESS
    Result = OpenPrinter(PrinterName, PrinterHandle, pd)
    Result = GetPrinter(PrinterHandle, 2, ByVal 0&, 0, Needed)
    ReDim pi2_buffer((Needed \ 4))
    Result = GetPrinter(PrinterHandle, 2, pi2_buffer(0), Needed, Needed)
    
    pFullDevMode = pi2_buffer(7)
    
    Call CopyMemory(MyDevMode, ByVal pFullDevMode, Len(MyDevMode))
    
    MyDevMode.dmDuplex = NewSetting
    MyDevMode.dmFields = DM_DUPLEX Or DM_ORIENTATION
    MyDevMode.dmOrientation = chng
    
    
    Call CopyMemory(ByVal pFullDevMode, MyDevMode, Len(MyDevMode))
    
    Result = DocumentProperties(Form1.hwnd, PrinterHandle, PrinterName, ByVal pFullDevMode, ByVal pFullDevMode, DM_IN_BUFFER Or DM_OUT_BUFFER)
    
    Result = SetPrinter(PrinterHandle, 2, pi2_buffer(0), 0&)
    
    Call ClosePrinter(PrinterHandle)
    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
    Exit Sub
10
    MsgBox Err.Number & " " & Err.Description
End Sub

Public Sub SetPrinterJob(PrinterName As String, PrinterQuality As Long, PrinterQuantaty As Long, PrinterOrentation As Long, PrinterTray As Long)
' On Error GoTo 10
    Dim PrinterHandle As Long
    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
    Dim i As Long
    
    If PrinterName = "" Then
        Exit Sub
    End If
    OldPrinterName = Printer.DeviceName
    pd.pDatatype = vbNullString
    pd.pDevMode = 0&
    pd.DesiredAccess = PRINTER_ALL_ACCESS
    Result = OpenPrinter(PrinterName, PrinterHandle, pd)
    Result = GetPrinter(PrinterHandle, 2, ByVal 0&, 0, Needed)
    ReDim pi2_buffer((Needed \ 4))
    Result = GetPrinter(PrinterHandle, 2, pi2_buffer(0), Needed, Needed)
    pFullDevMode = pi2_buffer(7)
    
    Call CopyMemory(MyDevMode, ByVal pFullDevMode, Len(MyDevMode))
    
    OldPrinterTray = Printer.PaperBin
    OldPrinterOrentation = Printer.Orientation
    OldPrinterQuality = Printer.PrintQuality
    OldPrinterQuantaty = Printer.Copies
    
    MyDevMode.dmDeviceName = Printer.DeviceName
    MyDevMode.dmOrientation = PrinterOrentation
    MyDevMode.dmDefaultSource = PrinterTray
    MyDevMode.dmPrintQuality = PrinterQuality
    MyDevMode.dmCopies = PrinterQuantaty
    MyDevMode.dmDuplex = DMDUP_SIMPLEX
    Call CopyMemory(ByVal pFullDevMode, MyDevMode, Len(MyDevMode))
    Result = DocumentProperties(Form1.hwnd, PrinterHandle, PrinterName, ByVal pFullDevMode, ByVal pFullDevMode, DM_IN_BUFFER Or DM_OUT_BUFFER)
    Result = SetPrinter(PrinterHandle, 2, pi2_buffer(0), 0&)
    
    Call ClosePrinter(PrinterHandle)
    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
    Exit Sub
10
    MsgBox Err.Number & " " & Err.Description


End Sub
when i call this, for some reason the first time it doesn't register, then if i call it the second time and each subsiquint time, it works. If i have some errors in my code that i didn't catch, let me know, i am still in the design phase, but am moving into the deployment stage soon. Thanks,
James
[shadeshappy] [evil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top