
Printing to non-default printer
I am not entirely sure - but I don't think that you can prevent the
Commondialog control from changing the Windows default printer without also
preventing your program from using the VB Printer Object to print to the
selected printer. The Help files say that If you set PrinterDefault to false
then you must use Windows GDI stuff to print to the .hdc property. The way I
overcome the problem is to write my own Printer Dialog Box. This will allow
you to use the Set Printer command to change the VB printer for your
application without changing the Windows default. The simple example below
only allows the user to change the Printer (not any of its properties) - but
you can easily extend it if you need to include other options. (By the way,
with
Visual Basic 5.0 there is a little problem with the Set Printer
command - but Service Pack 3 fixes it - so if you haven't already done so
then you will need to install Service Pack 3):
1. Create an additional form to act as your user dialogue (Form2 in this
example).
2. On your main form (Form 1) place a command button and use the following
code in its click event:
Private Sub Command1_Click()
Form2.Show VbModal
' When the user closes the modal Form by clicking OK then all
' subsequent printer output will go to the printer chosen by the user
End Sub
3. On Form 2 place a Combo Box and a Command Button. Set the Style property
of the Combo Box to "2 - DropDown List"
4. In Form2 Activate event use the following code:
Private Sub Form_Activate()
Dim printerobject As Printer
Dim element As Integer
Combo1.Clear
For Each printerobject In Printers
Combo1.AddItem printerobject.DeviceName
If Printer.DeviceName = printerobject.DeviceName Then
Combo1.ListIndex = element
End If
element = element + 1
Next printerobject
End Sub
5. In Form2 Command1 click event use the following:
Private Sub Command1_Click()
' Set the printer for this application only
Set Printer = Printers(Combo1.ListIndex)
Form2.Hide
End Sub
The important bit, as you will see, is the code in the Form2.Activate event.
Your job will be much simpler if you want to have your program (rather than
the user) choose the printer. You will not need to have a Form2 (as in the
example above) - just simply use something like the following each time you
want to change to a different printer:
Dim printerobject As Printer
For Each printerobject In Printers
If printerobject.DeviceName = "Some Other Printer" Then
Set Printer = printerobject
Exit For
End If
Next printerobject
Note that using a simple string comparison (as shown) means that you must
specify the Devicename EXACTLY (and it is case sensitive).
Mike Williams
Quote:
>I want to use commondialog.showprinter to choose which printer to print
>to from my vb program. However if I use the printerdefault = true
>value, it prints fine but changes the windows default printer. If I use
>the printerdefault = false, it prints to the default printer and not the
>one I pick in the showprinter dialog.
>Can someone tell me the proper method for using the showprinter dialog
>to specify printing to a non-default printer, without changing the
>windows default printer?