Solved

VB Printing with Commondialog and Printer Object

Posted on 1998-06-19
10
337 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:tward
10 Comments
 
LVL 2

Expert Comment

by:swilt
Comment Utility
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
0
 
LVL 7

Author Comment

by:tward
Comment Utility
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...
0
 

Expert Comment

by:vamsi_k
Comment Utility
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

0
 
LVL 7

Author Comment

by:tward
Comment Utility
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.
0
 

Expert Comment

by:vamsi_k
Comment Utility
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

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Author Comment

by:tward
Comment Utility
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.
0
 
LVL 7

Author Comment

by:tward
Comment Utility
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!!!
0
 
LVL 1

Expert Comment

by:cip
Comment Utility
This usually implies interference from code that is in the middle. Can you isolate a
defecting code between the two msgBox calls?
0
 
LVL 5

Accepted Solution

by:
dirtdart earned 100 total points
Comment Utility
Before you call the Common Dialog box, add the line:

Printer.TrackDefault = True

This causes the printer to keep track of the printer selected.
0
 
LVL 7

Author Comment

by:tward
Comment Utility
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!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now