Link to home
Start Free TrialLog in
Avatar of tward
tward

asked on

VB Printing with Commondialog and Printer Object

The following code used to work in VB 4.0:

CommonDialog1.CancelError = False
CommonDialog1.ShowPrinter

Printer.Print "Testing....."
Printer.EndDoc

Now in VB 5.0 it ALWAYS prints to the Default Printer no matter what
the user picked in the CommonDialog Box......

Is there a work around for this other than changing everything
to use the Windows API GDI Functions?
Avatar of swilt
swilt

I've got Sevice Pack 3 installed (Cmmom Dialog was updated in SP2)
Your code works fine, however it sets the selected printer to the default
Avatar of tward

ASKER

I also have Service Pack 3 installed...  Should have tested the code after I hacked it up and posted it!!!!

You are correct the above works.  It doesn't seem to work if you have a button on a form call btnSetup:

Private Sub btnSetup_Click()
 
  CommonDialog1.CancelError=False
  CommonDialog1.ShowPrinter

End Sub

And then you do the printing from another button:

Private Sub btnPrint_Click()

  Printer.Print "Testing....."

End Sub

The above only seems to print to the Default Printer that was initially setup, not whatever the user changes the printer to...
I have tried the following code just to verify whether it will print to the printer I selected in the Commondialog box, and it worked.

Private Sub Form_Load()
CommonDialog1.Action = 5
Printer.Print "Testing page"
End Sub

I have selected another printer other than the default printer and it printed to that printer.  Just check again.  It will work.

Regards,
Vamsi

Avatar of tward

ASKER

You are correct, I posted it the wrong way the first time!  The comment I added right before you answered it is actually the code that I am using.

It doesn't seem to work if you do the printer selection in a Function/Sub other than the one you print in.
I have done this as follows and it is working here.  In the Form load procedure, I displayed the printer dialog box, and selected one printer.  Then in button procedure, I again called the printer dialog box, and it is showing the printer which i have selected.  On clicking on "OK" in the dialog box, it is printing to that selected printer.

Private Sub Command1_Click()
CommonDialog1.ShowPrinter
Printer.Print "Testing page"
End Sub

Private Sub Form_Load()
CommonDialog1.ShowPrinter
End Sub

I don't know, why it is giving problems to u.  All I can assure is that the above code works, and is working here.

Regards,
Vamsi

Avatar of tward

ASKER

It may work from the Form_Load event but I don't want a printer dialog to appear unless the user wants it to appear by clicking on the printer setup button.
Avatar of tward

ASKER

This is the Exact code for a button in a large program:

Call MsgBox("Printer=" & Printer.DeviceName)
 
CommonDialog1.CancelError = False
 
CommonDialog1.ShowPrinter
 
Call MsgBox("Printer=" & Printer.DeviceName)

it doesn't work in that large program (both MsgBox calls display the same thing).

It does however work in a small test program (the last MsgBox displays the Printer chosen in the ShowPrinter line)....

This is very confusing...  Noting that this did not start happening until I upgraded the project to VB 5.0 from VB 4.0!!!
This usually implies interference from code that is in the middle. Can you isolate a
defecting code between the two msgBox calls?
ASKER CERTIFIED SOLUTION
Avatar of dirtdart
dirtdart

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 tward

ASKER

That did the trick, thanks a bunch!!  Just didn't dawn on me that something like that would have changed by just upgrading from VB 4.0 to VB 5.0.

Again, Thanks!