Solved

VB Printing with Commondialog and Printer Object

Posted on 1998-06-19
10
344 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
ID: 1463798
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
ID: 1463799
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
ID: 1463800
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 7

Author Comment

by:tward
ID: 1463801
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
ID: 1463802
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
 
LVL 7

Author Comment

by:tward
ID: 1463803
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
ID: 1463804
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
ID: 1463805
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
ID: 1463806
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
ID: 1463807
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

831 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