Excel VBA Tips: Selecting the Default Printer in VBA
I had created a document that is to be printed by multiple people in different locations. However, when pressing the print button that I created on the worksheet, I needed to ensure that the default printer for each location was selected. This can be done with the following code:
Sub DefaultPrint()
activePrint = Application.ActivePrinter
‘This sets the variable as the active printer that is currently selected in excel.
‘INSERT CODE HERE
Application.ActivePrinter = activePrint
‘This ensures that the code has not changed the default printer
ActiveWindow.SelectedSheets.PrintOut Copies:=1
‘The print command. There are multiple ways to do this
MsgBox “This has been sent to your active printer: ” & Application.ActivePrinter
‘Displays a message to the user so they know which printer it has been sent to.
End Sub
The above code will ensure that when printing, it is sent to the default printer for any user that prints from the worksheet.
Come check out the directory for the rest of my excel tips!
Submeg
IT
—————————
Microsoft Visual Basic for Applications
—————————
Compile error:
Variable not defined
—————————
Error on AtcivePrint = ………
LikeLike
But works good with: “ActivePrinter” in lieu of “activePrint”
LikeLike
Interesting! Perhaps different versions of excel cause different issues? Which version are you using?
LikeLike
Does this work for the default printer, or the last printer used in excel?
I’m having trouble printing to a default printer via VBA after printing to a different printer. It seems like Activeprinter is not the default, but the last printer used through excel…
LikeLike
When you mean the last used printer, had you printed anything earlier in the day or was it printed the day before, prior to turning the computer on for the day?
LikeLike
As it acts just like my quick print button. It prints to the last printer used, or default on start up.
LikeLike