Printing with VBA

Asator

Board Regular
Joined
Apr 5, 2010
Messages
186
I know there's a few different ways to store the default printer as a string, but is there a way I can then prompt the user from a popup with a list of printers, and set their selection as the default printer?

application.dialogs(xldialogprintersetup).show only returns true when they select a printer. I can use wscript.network to list all printers and set a default printer, but I don't think there's a way to have it return the default printer (in UNC format).

TL;DR - I want to store the current default printer, prompt the user with a list of installed printers, set their selection as default, then reset the printer to their pre-macro default when done.

Is this possible?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hrm. Is there a way to select printers from a drop down menu and have it return that selection as a string?
 
Upvote 0
It changes the active printer in Excel, but it doesn't change the default printer.

Basically I have an Outlook macro that archives and prints attachments (PDF files, word documents, excel files, etc). I wanted to allow the user to select which printer they want to send to.

Hmm.. thinking out loud here...

Basically I could maybe call excel's printer setup dialog, then store the result of application.activeprinter (subtracting the " on NeXX") before and after the xlDialogPrinterSetup, set the default printer using wscript.network using that string, do my printing, then set it back to the original.

Will test and report.
 
Upvote 0
Hmm... Any idea why this would fail? Get the error "Unable to get the Show property of the Dialog class".

Test code:

Code:
Sub setprinter()
Dim xlApp As Object
Dim prConfig As Object
On Error GoTo fail
Set xlApp = CreateObject("excel.application")
Set prConfig = CreateObject("wscript.network")
    prOld = Left(xlApp.ActivePrinter, InStr(xlApp.ActivePrinter, " on Ne") - 1)
    
    Do Until prResponse = True
        prResponse = xlApp.Dialogs(xlDialogPrinterSetup).Show
    Loop
    
    prNew = Left(xlApp.ActivePrinter, InStr(xlApp.ActivePrinter, " on Ne") - 1)
    
MsgBox prOld & " --> " & vbNewLine & prNew
fail:
If Err.Number <> 0 Then
    MsgBox "err: " & Err.Number & vbNewLine & Err.Description
End If
Set xlApp = Nothing
Set prConfig = Nothing

End Sub
 
Upvote 0
If you are late binding, Excel's constants won't be exposed, so you need to use the value of xlDialogPrinterSetup which is 9:

Code:
prResponse = xlApp.Dialogs(9).Show
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top