Link to home
Start Free TrialLog in
Avatar of JodiC
JodiC

asked on

set printer

I want to catch and restore the default printer from the
common dialog box after the user has finished printing to
a second printer.  Please see my code example:

Dim p As Printer
Dim OldPrinter As Printer

For Each p In Printers
    If p.DeviceName = Printer.DeviceName Then
        Debug.Print "loop "; p.DeviceName
       ' Set printer as system default.
        Set OldPrinter = p
        ' Stop looking for a printer.
        Exit For
    End If
Next

    OldFont = Printer.Font
    CommonDialog1.Flags = cdlPDPrintSetup
    CommonDialog1.Flags = cdlPDHidePrintToFile
    CommonDialog1.PrinterDefault = True
    On Error Resume Next
    CommonDialog1.CancelError = True
    CommonDialog1.ShowPrinter
    If Err.Number = cdlCancel Then
      Exit Sub
    Else
      If Err.Number <> 0 Then
        MsgBox Err.Description
      End If
    End If
    On Error GoTo 0
    .
    .
Printer.EndDoc
Set Printer = OldPrinter
Debug.Print "set printer "; Printer.DeviceName
Printer.FontName = OldFont

The immidiate window shows me that I am finding the default
printer and then resetting it after EndDoc.  However, when
I select print again or exit the window and come back, the
printer is the last printer selected not the default I
set with set printer.  I'm a newbie, thanks for any help.
ASKER CERTIFIED SOLUTION
Avatar of waty
waty
Flag of Belgium 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 JodiC
JodiC

ASKER

Well, yes that did save and reset the printer.  However, now
when I select a printer other than the default it does not print
to the selected printer.  That worked before, it does not now.
I have little doubt it is something I am doing.  Here's the code:

    PrinterName = Printer.DeviceName
    Debug.Print "printer name "; PrinterName
    OldFont = Printer.Font
    CommonDialog1.Flags = cdlPDPrintSetup
    CommonDialog1.Flags = cdlPDHidePrintToFile
    CommonDialog1.PrinterDefault = True
    On Error Resume Next
    CommonDialog1.CancelError = True
    CommonDialog1.ShowPrinter
    If Err.Number = cdlCancel Then
      Exit Sub
    Else
      If Err.Number <> 0 Then
        MsgBox Err.Description
      End If
    End If
    On Error GoTo 0
     
    Printer.FontName = "Courier New"
    Printer.Orientation = vbPRORPortrait
   
    Open Order_File For Input As #1
    Do While Not EOF(1)
       Line Input #1, Textline
       Printer.Print Textline
     Loop
     Close #1

Printer.EndDoc
Win95SetDefaultPrinter (PrinterName)
Debug.Print "set printer "; Printer.DeviceName
Printer.FontName = OldFont
Exit Sub





Avatar of JodiC

ASKER

It appears that the routine Waty sent me works just fine to set
the printer back.  The trouble I think the problem I am having
is that the printer does not change if I do not change the
selection in the dialog box.  That is if I leave the default
it is printing to the last selection even though the dialog box
is correct.  What must I do to set my printer to the selection
displayed or chosen from the dialog
Avatar of JodiC

ASKER

Waty's answer to my original question is correct.  I'm not sure
how to sink up the printer with the dialog selection still.  If
I exit the window and reenter it seems to work o.k.  That will
do for now.

Thanks for your help