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

common dialog - setting default printer

Status
Not open for further replies.

FiveRand

Programmer
Apr 30, 2003
5
ZA


Hi I am using the common dialog to print documents, the user can select the printer which is fine. My problem is that the once the dialog is cloed then the printer that is selected is set to the dedault printer. This is the problem

In my Form_Load()

Printer.TrackDefault = True

in the method where i print I have the following:


storePrinter = Printer.DeviceName
MsgBox storePrinter
CommonDialog1.ShowPrinter

'''this is where I do the printing'''
the storeprinter returns the origional name of the default printer


after printing I have the following method to try set the default printer back to the origional default.

Sub SetPrinter()

Dim Pr As Printer
For Each Pr In Printers
If UCase(Pr.DeviceName) = UCase(storePrinter) Then
Set Printer = Pr
Exit Sub
End If
Next

End Sub

I am not sure why the default printer is not set to the origional default. Any help would be *greatly* appreciated!!

thanx in advance
 
As I recall after CommonDialog1.ShowPrinter is executed the trackdefault gets set to false which prevents the default printer being changed again. My fix was to put the line Printer.TrackDefault = True after the .ShowPrinter, in your case at the start of your setprinter rtn would probably do it.

Hope this helps.
 
Hello Guys...

I am having the very same problem. Did anyone find the solution?

I can not get the default printer back. My code is the same as above..

Dim storeprinter
Printer.TrackDefault = True
storeprinter = Printer.DeviceName
Printer.TrackDefault = True 'have tried here
CommonDialog1.ShowPrinter
Printer.Print "TEST"
Printer.EndDoc
Printer.TrackDefault = True 'and here
Dim pr As Printer
For Each pr In Printers
If UCase(pr.DeviceName) = UCase(storeprinter) Then
Set Printer = pr
Exit Sub
End If
Next pr
Screen.MousePointer = vbDefault

Hope this isn't a lost thread/cause

Thanks

[blues]

 
If you get really stuck you could try PDXPro; an ActiveX DLL. A trial copy can be downloaded from my web site. It just takes 3 lines of code to store/restore the default printer:

Set objPDM = CreateObject("PDXPro.PDManager"
strDefaultPrinter = objPDM.DefaultPrinterName
'show the print dialog
CommonDialog1.ShowPrinter
'...
'restore the default printer
objPDM.SetDefaultPrinter strDefaultPrinter

Paul Bent
Northwind IT Systems
 

Using the common dialog defaultprinter = true changes the systems default printer while...
[tt]
Set Printer = PrinterObject
[/tt]
will only set the default printer for the visual basic program. So ...
[tt]
Dim P As Printer, S As String

S = Printer.DeviceName

CD.PrinterDefault = True
CD.ShowPrinter

'do printing

For Each P In Printers
If P.DeviceName = S Then
Set Printer = P
Exit For
End If
Next
[/tt]

Will change the default printer of the system and then change the default printer for the visual basic program back to the origional default printer leaving the systems default printer changed. To avoid this (or set the systems default printer back to the origional) use the SetDefaultPrinter API. See Thread222-364736

Good Luck

 
vb5prgrmr,

That's really what I want to only change the printer for the visual basic program.

Thanks for your reply and suggestion. I will check it out and get back to you.

 
vb5prgrmr,

I cut an pasted your code but must be missing something.

Dim P As Printer, S As String
S = Printer.DeviceName
CommonDialog1.PrinterDefault = True
CommonDialog1.ShowPrinter
Printer.Print "TEST"
Printer.EndDoc
For Each P In Printers
If P.DeviceName = S Then
Set Printer = P
Exit For
End If
Next p

What is happening, from the cd.showprinter I select an other printer (not the system default) to print from the program. But the printout still goes to the orginal default printer and upon exit the loop the selected printer becomes the default.

S has the orginal default printer.

The first pass of the loop P.DeviceName is the selected printer, selected thru the dialog box and it hits the

Set Printer = P

The system default printer changes to the one selected from the dialog box.

I'm at a lost, If possible I do not want to change the system default printer, Just to print a report to an other printer.

The API you link points to seems simple enough but doesn't it change the system default printer? or should I call it twice,

1st time before printing: SetDefaultPrinter=Printer.DeviceName
.....Print stuff

after printing
2nd time
from within the loop

If P.Device=S then
SetDefaultPrinter=S
end if

Mondays should be called BrainDead day

Thanks
Jerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top