Print Dialog Set Printer Name
I am printing from a module so I can't use the CommonDialog ocx so I am using the PrintDialog API.
All I wan't to do is let the user choose the printer to print to.
I'm not interested in any of the other flags or options.
The function should only set the name of the new printer chosen.
I have taken the AllAPI Common Dialogs example and stripped it down to its bare minimum (I think)
It still looks a bit over the top to me.
Is there anything else I can remove from this code? (I don't fully understand what is happening)
Or is there a better way of doing this?
VB Code:
Option Explicit
Const CCHDEVICENAME = 32
Const CCHFORMNAME = 32
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 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 Sub Form_Load()
'KPD-Team 1998
'URL: [url]http://www.allapi.net/[/url]
Command5.Caption = "ShowPrinter"
End Sub
Private Sub Command5_Click()
ShowPrinter Me
End Sub
Public Sub ShowPrinter(frmOwner As Form, Optional PrintFlags As Long)
'-> Code by Donald Grover
Dim PrintDlg As PRINTDLG_TYPE
Dim DevMode As DEVMODE_TYPE
Dim lpDevMode As Long
Dim objPrinter As Printer
Dim NewPrinterName As String
' Use PrintDialog to get the handle to a memory
' block with a DevMode and DevName structures
PrintDlg.lStructSize = Len(PrintDlg)
PrintDlg.hwndOwner = frmOwner.hWnd
PrintDlg.flags = PrintFlags
On Error Resume Next
'Call the print dialog up and let the user make changes
If PrintDialog(PrintDlg) <> 0 Then
'Next get the DevMode structure and set the printer
'properties appropriately
lpDevMode = GlobalLock(PrintDlg.hDevMode)
CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
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
'set printer toolbar name at this point
End If
Next
End If
On Error GoTo 0
End If
MsgBox NewPrinterName
End Sub
Re: Print Dialog Set Printer Name
You could make a small form with a combo box and fill it with
VB Code:
Dim p As Printer
For Each p In Printers
cmbPrinters.Add(p.DeviceName)
Next p
Re: Print Dialog Set Printer Name
It doesn't look right if you do that though.
Unless you spend ages making the form look like the print dialog.