Re: Setting Default Values for Printer
- From: "Mike Williams" <Mike@xxxxxxxxxxxxxxxxx>
- Date: Sun, 25 Dec 2005 09:29:17 -0000
"Doug van Vianen" <courses@xxxxxxxx> wrote in message news:efPUUiRCGHA.2036@xxxxxxxxxxxxxxxxxxxxxxx
However, I recently changed to a new computer with WindowsXP and the coding no longer works. Instead it presents a box with the message "To change the default device settings select 'Printer Preferences' rather than 'Properties' when right clicking on the printer icon in the printer's folder."
Windows XP is very fussy Doug about the access it gives you. On my own system your code just displays the "Utilities" tab of the printer dialog, allowing me to do nothing else other than perform jet cleaning or other printer "housework". Also, the VB printer object has its own idiosyncrasies, which complicates matters further. There are various ways around the problem. My own preferred way is to forget all about the VB printer object and to instead print directly to the device context (hDC) that is returned by the CommonDialog Control, but that method of course requires you to do all your printing using the various API text and graphic methodsm which is a fair bit of extra work (as far as coding is concerned). Another method which works quite well is to use the VB Set Printer method to tell the VB printer object which printer to use (this does not mess about with the Windows default and therefore XP does not present a problem). In order to do this you need to also use the API routines to give the user a choice of settings which you can then transfer to the VB printer object. Here is an example of the latter method. Basically it is a simplification and slight modification of some similar code that you will find on the MSDN knowledge base site. The following modified example works quite well. Paste it into a VB Form containing a Command Button.
Merry Christmas :-)
Mike
Option Explicit Const OUT_DEFAULT_PRECIS = 0 Const CLIP_DEFAULT_PRECIS = 0 Const DEFAULT_QUALITY = 0 Const DEFAULT_PITCH = 0 Const FF_ROMAN = 16 Const CCHDEVICENAME = 32 Const CCHFORMNAME = 32 Const GMEM_MOVEABLE = &H2 Const GMEM_ZEROINIT = &H40 Const DM_DUPLEX = &H1000& Const DM_ORIENTATION = &H1& Const PD_PRINTSETUP = &H40 Const PD_DISABLEPRINTTOFILE = &H80000 Private Type POINTAPI x As Long y As Long End Type Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type PRINTDLG_TYPE lStructSize As Long hwndOwner As Long hDevMode As Long hDevNames As Long hDC As Long flags As Long nFromPage As Integer nToPage As Integer nMinPage As Integer nMaxPage As Integer nCopies As Integer hInstance As Long lCustData As Long lpfnPrintHook As Long lpfnSetupHook As Long lpPrintTemplateName As String lpSetupTemplateName As String hPrintTemplate As Long hSetupTemplate As Long End Type Private Type DEVNAMES_TYPE wDriverOffset As Integer wDeviceOffset As Integer wOutputOffset As Integer wDefault As Integer extra As String * 100 End Type Private Type DEVMODE_TYPE 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 Private Declare Function PrintDialog Lib "comdlg32.dll" _ Alias "PrintDlgA" (pPrintdlg As PRINTDLG_TYPE) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias _ "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, _ ByVal cbCopy As Long) Private Declare Function GlobalLock Lib "kernel32" _ (ByVal hMem As Long) As Long Private Declare Function GlobalUnlock Lib "kernel32" _ (ByVal hMem As Long) As Long Private Declare Function GlobalAlloc Lib "kernel32" _ (ByVal wFlags As Long, ByVal dwBytes As Long) As Long Private Declare Function GlobalFree Lib "kernel32" _ (ByVal hMem As Long) As Long
Private Sub ShowPrinter(frmOwner As Form, Optional _
InitialPrinter As String, Optional PrintFlags As Long)
Dim PrintDlg As PRINTDLG_TYPE
Dim DevMode As DEVMODE_TYPE
Dim DevName As DEVNAMES_TYPE
Dim lpDevMode As Long, lpDevName As Long
Dim bReturn As Integer
Dim objPrinter As Printer, NewPrinterName As String
PrintDlg.lStructSize = Len(PrintDlg)
PrintDlg.hwndOwner = frmOwner.hWnd
PrintDlg.flags = PrintFlags
On Error Resume Next
' Set the initial printer in the dialog
If Len(InitialPrinter) > 0 Then
For Each objPrinter In Printers
If InStr(1, objPrinter.DeviceName, InitialPrinter, _
vbTextCompare) > 0 Then
Set Printer = objPrinter
End If
Next
End If
'
DevMode.dmDeviceName = Printer.DeviceName
DevMode.dmSize = Len(DevMode)
DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX
DevMode.dmPaperWidth = Printer.Width
DevMode.dmOrientation = Printer.Orientation
DevMode.dmPaperSize = Printer.PaperSize
DevMode.dmDuplex = Printer.Duplex
On Error GoTo 0
PrintDlg.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or _
GMEM_ZEROINIT, Len(DevMode))
lpDevMode = GlobalLock(PrintDlg.hDevMode)
If lpDevMode > 0 Then
CopyMemory ByVal lpDevMode, DevMode, Len(DevMode)
bReturn = GlobalUnlock(PrintDlg.hDevMode)
End If
With DevName
.wDriverOffset = 8
.wDeviceOffset = .wDriverOffset + 1 + Len _
(Printer.DriverName)
.wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port)
.wDefault = 0
End With
With Printer
DevName.extra = .DriverName & Chr(0) & _
.DeviceName & Chr(0) & .Port & Chr(0)
End With
PrintDlg.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or _
GMEM_ZEROINIT, Len(DevName))
lpDevName = GlobalLock(PrintDlg.hDevNames)
If lpDevName > 0 Then
CopyMemory ByVal lpDevName, DevName, Len(DevName)
bReturn = GlobalUnlock(lpDevName)
End If
If PrintDialog(PrintDlg) <> 0 Then
lpDevName = GlobalLock(PrintDlg.hDevNames)
CopyMemory DevName, ByVal lpDevName, 45
bReturn = GlobalUnlock(lpDevName)
GlobalFree PrintDlg.hDevNames
lpDevMode = GlobalLock(PrintDlg.hDevMode)
CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
bReturn = GlobalUnlock(PrintDlg.hDevMode)
GlobalFree PrintDlg.hDevMode
NewPrinterName = UCase$(Left(DevMode.dmDeviceName, _
InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
If Printer.DeviceName <> NewPrinterName Then
For Each objPrinter In Printers
If UCase$(objPrinter.DeviceName) = NewPrinterName Then
Set Printer = objPrinter
End If
Next
End If
On Error Resume Next
Printer.Copies = DevMode.dmCopies
Printer.Duplex = DevMode.dmDuplex
Printer.Orientation = DevMode.dmOrientation
Printer.PaperSize = DevMode.dmPaperSize
Printer.PrintQuality = DevMode.dmPrintQuality
Printer.ColorMode = DevMode.dmColor
Printer.PaperBin = DevMode.dmDefaultSource
On Error GoTo 0
End If
End SubPrivate Sub Command1_Click() ' Display a printer dialog which starts off with any ' initial printer we wish. In this example we use the ' first Epson printer that we find. If the specified ' printer is not found then the dialog opens showing ' the current default printer (If you want to start ' showing the default then you can either modify the ' routine so that the first For Each loop is not ' executed or, more simply, use something like: ' ShowPrinter, Me, "start with the default printer" ' ShowPrinter Me, "Epson" Printer.Print "Whisky and Coke is a wonderful drink." Printer.EndDoc ' End Sub
.
- References:
- Setting Default Values for Printer
- From: Doug van Vianen
- Setting Default Values for Printer
- Prev by Date: Re: viusal basic 6 - Treeview right to left
- Next by Date: why no form be shown in task bar?
- Previous by thread: Setting Default Values for Printer
- Next by thread: Re: Setting Default Values for Printer
- Index(es):
Loading