Option Explicit
Private Sub cmdShowPrinter_Click()
'Means our changes won't be set as default system-wide
'cmnPrinter.PrinterDefault = False
'set an API timer - this will allow an event to be raised even though
'the printer dialog box will have complete control over our thread
'Call timeSetEvent(500, 100, AddressOf TimerProc, 0, TIME_ONESHOT)
'Wait half a second, with a resolution of 100.
'Call functin TimerProc, no user-data being
'passed, and call it only once.
'show our printer selection dialog box
'cmnPrinter.ShowPrinter
Dim printDlg As PrinterDlg
Set printDlg = New PrinterDlg
' Set the starting information for the dialog box based on the current
' printer settings.
printDlg.PrinterName = Printer.DeviceName
printDlg.DriverName = Printer.DriverName
printDlg.Port = Printer.Port
' Set the default PaperBin so that a valid value is returned even
' in the Cancel case.
printDlg.PaperBin = Printer.PaperBin
' Set the flags for the PrinterDlg object using the same flags as in the
' common dialog control. The structure starts with VBPrinterConstants.
printDlg.Flags = VBPrinterConstants.cdlPDNoSelection _
Or VBPrinterConstants.cdlPDNoPageNums _
Or VBPrinterConstants.cdlPDReturnDC
Printer.TrackDefault = False
' When CancelError is set to True the ShowPrinterDlg will return error
' 32755. You can handle the error to know when the Cancel button was
' clicked. Enable this by uncommenting the lines prefixed with "'**".
'**printDlg.CancelError = True
' Add error handling for Cancel.
'**On Error GoTo Cancel
Call timeSetEvent(500, 100, AddressOf TimerProc, 0, TIME_ONESHOT)
If Not printDlg.ShowPrinter(Me.hwnd) Then
Debug.Print "Cancel Selected"
Exit Sub
End If
'Turn off Error Handling for Cancel.
'**On Error GoTo 0
Dim NewPrinterName As String
Dim objPrinter As Printer
Dim strsetting As String
' Locate the printer that the user selected in the Printers collection.
NewPrinterName = UCase$(printDlg.PrinterName)
If Printer.DeviceName <> NewPrinterName Then
For Each objPrinter In Printers
If UCase$(objPrinter.DeviceName) = NewPrinterName Then
Set Printer = objPrinter
Set objPrinter = Nothing
End If
Next
End If
Debug.Print Printer.DeviceName
Debug.Print Printer.hDC
End Sub