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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Dialog changes the default printer

Status
Not open for further replies.

ndp

Programmer
Mar 3, 2003
121
US
I am using Common Dialog control(VB6) for letting the user choose the Printer. I do the print jobs in my program through Printer object.
It works fine, but when user selects a printer, it makes it default printer on that machine. I don't want to change their original set up. I just want to change the printer and properties during the program only. Once they exit my program, it should come to original printer settings on their machine.

Thanks
ndp
 
NOT TESTED!

Store default printer before changing it.

Printer.DeviceName

After changing it . .

Dim x As Printer

For Each x In Printers
'If deviceName is the same as the stored above then
exit the for keeping track of the index
Debug.Print x.DeviceName

Next

Set Printer = Printers(n)

[flowerface]

"All I ask is the chance to prove that money can't make me happy." - Spike Milligan
 
You are pointing me exactly where I am looking for!

I had tried something like that to get the devicename, but how do I get the value of n in printers collection for that perticular printer(devicename)? For example, if the user selects a zzz printer. But then how do I know what will be number in Printers collection so that I can set Printer = Printers(n) for the zzz printer?

Thanks
-ndp
 
I think "tb" meant

For Each X In Printers
If X.DeviceName = DefaultDevice then
Set Printer = X
Exit For
End If
Next X

The syntax he (or she?) has provided would be appropriate if you looped through the printer collection with something like

For n = 1 to Printers.Count
 
I actually got that "as is" from MSDN. (FYI)

>'If deviceName is the same as the stored above then
exit the for keeping track of the index

I meant excatly that >For n = 1 to Printers.Count
just suggested to declare a counter and incrementing it every time in the for each ... next loop.

[flowerface]

"All I ask is the chance to prove that money can't make me happy." - Spike Milligan
 
I tried this to test...

dim strDevicename As String
Private Sub cmdPrint_Click()
Dim prt As Printer
strDevicename = Printer.DeviceName
CommonDialog1.ShowPrinter
Printer.Print "This is test"
Printer.EndDoc
End Sub

Private Sub cmdExit_Click()
Dim p As Integer
For p = 1 To Printers.Count
If Printers(p).DeviceName = strDevicename Then
Set Printer = Printers(p)
Exit For
End If
Next
End Sub

In order to print to selected printer from printer dialog, I needed to set the PrinterDefault property of Printer Dialog to true. otherwise it prints to default printer. When I run the code above it prints from selected printer and makes it default printer, but when I exit(by pressing exit button) it should set the printer to the original printer which devicename is stored in strDevicename. It doesn't do that. May be I am missing something or my syntex is not correct.

I appreciate your help and guidance.
Thanks
ndp
 
ndp,

I had the same problem that you are having. You might what to check out thread222-553788. I never did get it to work properly within the code. What I had to do is to redisplay the CommonDialog1.showprinter again to let the user change the printer back. So, if you or anyone finds a solution I will like to know. So I can't be of more help.


[blues]
 

ndp,

The Thread222-553788 referenced by WZUP with the SetDefaultPrinter API may be of help to you, but there is a link in that thread to Thread222-364736 that explains the use of the SetDefaultPrinter API and what you have to do if you are not on a win2k system (see last post).

WZUP,

I noticed from this thread that you still have not been able to accomplish what you want and from the previous Thread222-553788 that your target machine was an NT system. So let me also point you to the last post in Thread222-364736.

Good Luck All

 
Thanks to vb5prgrmr, tb and WZUP!

You all pointed me in correct direction. The SetDefaultPrinter API works great! It works on client machine too!

Thanks again,
ndp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top