Printing to non-default printer 
Author Message
 Printing to non-default printer

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?



Sun, 24 Jun 2001 03:00:00 GMT  
 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?



Mon, 25 Jun 2001 03:00:00 GMT  
 Printing to non-default printer
set the printer object to be which ever printer you want from the printers
collection. This sets VBs defualt printer without setting the system defualt

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?



Mon, 25 Jun 2001 03:00:00 GMT  
 Printing to non-default printer
Hi Matthew,

You have 2 options:
    - use Mike Williams' suggestion and create your own PrintDialog;
    - call the PrintDlg() API directly to display the Windows Printer
Dialog. This way you can get the selection and settings made by the user and
use the Printers collection to setup the correct printer and use it for
printing.

If you need some sample code just Email me.

Regards,
                Stoil

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?



Sun, 01 Jul 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Q: Printing report on non-default printer w/o user selecting printer

2. Printer.Print on non-default printer

3. printer default to non-default printer

4. How selecting a (non-default) printer for printing a report

5. Printing to non-default printer

6. How do I Print to non-default printer in VB5

7. printing to non-default printer

8. Help w/ printing to non-default printer

9. Print on a non-default printer

10. Print to non-default printer

11. Macro that selects printer and prints doc also changes default printer

12. Printing to a printer that is NOT the default printer

 

 
Powered by phpBB® Forum Software