Solved

VB Printing with Commondialog and Printer Object

Posted on 1998-06-19
10
340 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 add a minute to the date time value 8 60
Windows 10 start screen issues 9 51
MS Access Search and Replace Using VBA 6 57
How to debug this code 7 56
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…

932 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

12 Experts available now in Live!

Get 1:1 Help Now