Link to home
Start Free TrialLog in
Avatar of jhamlett
jhamlett

asked on

Printing question

Hi,

I have a method that prints out a picture, and I am using the Printers collection in the code, along with the Printer object, like so:

frmMain.CommonDialog1.ShowPrinter
frmMain.CommonDialog1.Flags = cdlPDReturnDC
 
Dim Y As Printer
For Each Y In Printers
    If Y.hdc = frmMain.CommonDialog1.hdc Then
        ' Set printer as system default.
        Set Printer = Y
        ' Stop looking for a printer.
        Exit For
    End If
Next

The above works fine. Through this code, I aquire the printer that the user selected from the CommonDialog object. My problem is that now "Printer", which used to point to the default printer, now points to another printer, which has now become the default printer. Is there a way to set the default printer back to what it was before I changed it?

I have already tried the following:

"Dim DefaultPrinter as Printer
 Set DefaultPrinter = Printer

 'lines of code to do all my printing stuff
 Set Printer = DefaultPrinter"

This doesn't seem to work, DefaultPrinter never receives a value. Also tried this at the very begining of the method:

    Dim X As Printer
    For Each X In Printers
        If X.hdc = Printer.hdc Then
            Set DefaultPrinter = X
            Exit For
        End If
    Next
This never finds a match either. I am stumped now. Any help would be appreciated.


Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Just store the device name.

Then use code a bit like this:


StorePrinter = Printer.DeviceName

' Do your printing


CAll SetPrinter(StorePrinter)

' Etc....


Sub SetPrinter(DeviceName as string)

Dim Pr as Printer
For Each Printer in Printers
    If Ucase(Pr.DeviceName) = Ucase(DeviceName) Then
        Set Printer = Pr
        Exit Sub
    End If
Next

End Sub



I expect in your form load/Sub Main you have:

Printer.TrackDefault = True
ASKER CERTIFIED SOLUTION
Avatar of inthedark
inthedark
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jhamlett
jhamlett

ASKER

Both good answers. I tried intherdark's method, since it was the simplest, and I do get back the default device name. I seem like I am setting the printer i want back to default, until I re-enter the function and re-open the common dialog. at that point, it still shows the last printer selected as being default? Since I am getting the selected printer via the .hDc prop of the common dialog, is it likely that the common dialog is setting the default based on its own .hDc?
jhamlett, just an after thought.  When I develop an App. I get the op. to select the printers they want to use for invoices and also for reports.  So I always select the right printer for the job, then return it to the default after the report is complete.

The documentation covering the use is the common dialog in respect of printers is very poor.  The documentation expected you to be using the RTF control.....but why would I want to use that when most be use Word for WP?
I tried implementing this and it does not selet the current printer...

Anyone have an updated version of this type of code all in one place????