Link to home
Start Free TrialLog in
Avatar of WHITER
WHITER

asked on

Selecting A Printer Using The Printer Dialog Control

I am having real problems assigning a printer to an object.  I can show the print dialog box but it doesn;t matter which printer is then selected by the user the default is always used.  I know I must be missing something obvious....but what?
Avatar of cerik
cerik

do you do something like this

'Set flags to return hDC
'CommonDialog1.Flags = CommonDialog1.Flags + &H100
'this will cause a hDC to be returned
-------------
CommonDialog1.ShowPrinter
Debug.Print Printer.DeviceName
Printer.Print ""
Printer.EndDoc

the name of your selected printer should print in the debug window and a blank page
print out?

If this helps I will resubmit as an Answer
Avatar of WHITER

ASKER

Cerek,

It hasn't solved my problem, however I am struggling to come to terms with exactly whats going on here.  I have read the Microsoft literature and am still none the wiser as to what hDC actually does and how I use it.  

Your solution certainly caused an hDC to be returned but didn;t change my printer.devicename to the one selected.  Maybe I've missed the point somewhere?
Cerik, does that change the default windows printer globally ?
Avatar of WHITER

ASKER

Swilt/Cerik,

I do not have the printerdefault set to true, therefore any changes I make would be temporary.  
You could create your on printer selection form

Code for test form

Option Explicit

Private Sub Command1_Click()
    frmPrintSel.Show vbModal
    If Not frmPrintSel.bCancel Then
        Printer.Print Printer.DeviceName
        Printer.EndDoc
    End If
End Sub


Code for frmPrintSel
    List1 - Listbox to display printers
    cmdOK - button to select a printer
    cmdCancel to cancel the operation


Option Explicit

Public bCancel As Boolean

Private Sub Form_Load()
    Dim i As Integer
   
    bCancel = True
    If Printers.Count = 0 Then
        List1.AddItem "No printers installed"
        CmdOk.Enabled = False
    Else
        For i = 0 To Printers.Count - 1
            List1.AddItem Printers(i).DeviceName
        Next i
    End If
End Sub

Private Sub cmdCancel_Click()
    Unload Me
End Sub

Private Sub CmdOk_Click()
    Dim i As Integer
   
    For i = 0 To List1.ListCount - 1
        If List1.Selected(i) Then
            Set Printer = Printers(i)
            bCancel = False
            Exit For
        End If
    Next i
    If bCancel Then
        MsgBox "You have not selected a printer", vbCritical, "Printer Selection"
    Else
        Unload Me
    End If
End Sub


ASKER CERTIFIED SOLUTION
Avatar of web_crusher
web_crusher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WHITER

ASKER

Not bad Web_Crusher, However have you ever done this using the default printer dialog box provided with VB5?