[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VB Printing with Commondialog and Printer Object

Posted on 1998-06-19
10
Medium Priority
?
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

650 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