How do you change the default printer?

  • Thread starter Thread starter DennisE
  • Start date Start date
D

DennisE

I would like to from within Excel as an VBA instruction change the
name of the default printer, just as if I went to the Windows Control
Panel and did it. Incorporating in my code either of the instructions
Application.Dialogs(xlDialogPrint).Show or
Application.Dialogs(xlDialogPrinterSetup).Show
lets me change the printer for that job, but seems to leave the
name of the default printer the same as before. Any suggestions,
anyone?

-- Dennis Eisen
 
DennisE said:
I would like to from within Excel as an VBA instruction change the
name of the default printer, just as if I went to the Windows Control
Panel and did it. Incorporating in my code either of the instructions
Application.Dialogs(xlDialogPrint).Show or
Application.Dialogs(xlDialogPrinterSetup).Show
lets me change the printer for that job, but seems to leave the
name of the default printer the same as before. Any suggestions,
anyone?

You can use VBA to change the name of the ActivePrinter. The macro recorder
gives something like

Application.ActivePrinter = "Generic / Text Only on FILE:"

You could always put this in a BeforePrint event handler.

The default printer, on the other hand, is a Windows-wide configuration
setting. You may need to make a Windows API call to do this.
 
I assume you mean for Excel and not for all windows applications.

Look at help setting the ActivePrinter

(turn on the macro recorder and perform the action manually - that will also
be helpful).

I believe that and what you have shown are what are offered internally to
Excel and VBA. After that, you would probably need to use the Windows API.
 
this shows how to use the Windows API in VB to do it. It would need to be
adapted to VBA.

http://support.microsoft.com/default.aspx?scid=kb;en-us;266767
HOWTO: Set Which Printer Is the System Default Printer

Another approach might be to take a picture of the useform, put it on a
blank sheet and print the sheet.

Following code was posted previously by Orlando Magalhaes Filho:

In a general module put in these declarations/code:

Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Const VK_SNAPSHOT = &H2C

Sub Test()
UserForm1.Show
End Sub


In the code module of the Userform:

Private Sub CommandButton1_Click()
keybd_event VK_SNAPSHOT, 0, 0, 0
Workbooks.Add
Application.Wait Now + TimeValue("00:00:01")
ActiveSheet.PasteSpecial Format:="Bitmap", Link:=False, _
DisplayAsIcon:=False
ActiveSheet.Range("A1").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1
ActiveWorkbook.Close False
End Sub


This takes a "picture" of the userform and pastes it on a sheet (adds a
workbook with a sheet, prints, then closes the workbook without saving
change). It then prints the picture on the sheet.

Regards,
Tom Ogilvy
 
Back
Top