Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB printing via Api's

Status
Not open for further replies.

splaisance

Programmer
Jul 2, 2001
67
US
I do not have any experience with API's and have been trying to understand them with some luck. Unfortunately I am still stuck on how to pull the information together to correct my problem.

I have a VB program which prints a report using the visual basic printer object. The problem is that if I allow the users to choose a printer to print to via the common dialog box, it changes their default printer. If I don't allow them to change the printer - then the report, of course, printers on their default printer.


I want to allow the user to choose which network printer they want the report to print on BUT their default printer needs to either stay the same or be changed back. Can someone help me? I think I need to use a device context to return a handle to the printer but can't figure out exactly how to do this.


Many Thanks!!

Shannon





 
You might to try the following:

Printer.TrackDefault = True
lStr_DefPrtName = Printer.DeviceName

lStr_DefPrtName should contain the name of the default printer.

Allow your user to select the desired printer.
Print the Report.
After the .EndDoc then

Set Printer = lStr_DefPrtName

This should restore the default printer


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Thanks for the tip but it isn't what I am looking for.

I don't want to change the default printer at all.
If I change the default printer then all kinds of problems could be introduced. The program crashes and the printer isn't changed back, a user halts the program via something like task manager and the printer isn't changed back, or perhaps the user goes to another application (say Word ) and prints a document out before my program switches the default printer back?

I am sure this can be done since most applications allow you to do it - I am just stuck in finding the exact solution.


Any other ideas/suggestions/answers????

Shannon
 
Here's what I use:

Sub PrintStuff(strPrinter as String)
Dim prn As Printer
For Each prn In Printers
If UCase(prn.DeviceName) = UCase(strPrinter) Then
Set Printer = prn
Exit For
End If
Next
frmWhatever.PrintForm
End Sub

It doesn't change the default windows printer, and it works fine for me. You could use the printer object's device name property to retrieve all the names, and put them in a combo for the user to select from.
 
It looks like you may be able to use the CommonDialog control and set it's PrinterDefault property to false. Look up the PrinterDefault property in the MSDN.
 
Thanks for the input shmiller. I ended up using something similar to what you posted and it worked great.

If you set the PrinterDefault property to false on the CommonDialog control, printing to the printer object will still go to whatever is the default printer. I did read about it on MSDN and tried it to. So that route didn't work.


Thanks again for the feedback and help!


Splaisance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top