VBA to reset the default printer

colinharwood

Active Member
Joined
Jul 27, 2002
Messages
440
Office Version
  1. 365
Platform
  1. Windows
CreatePDF:
Application.ScreenUpdating = False
CreateObject("WScript.Network").SetDefaultPrinter "Microsoft Print to PDF"
'copy details & save to PDF file
Range("$A$1:$H$45").Select
ThisFile = Range("N5").Value & " " & Range("O5").Value
FileDrive = "C:\TMES Email Renewals\"
Selection.PrintOut Copies:=1, ActivePrinter:= _
"Microsoft print to pdf", Collate:=True, _
printtofile:=True, prtofilename:=FileDrive & ThisFile & ".pdf"
Sheets("PDF Membership Card").Range("S1").Select

When using this code , It changes the default printer to "Microsoft Print to PDF". Is there a way to get the name of the original default printer, to use as a variable , to reset the default printer after printing to "Microsoft Print to PDF"
Thanks
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You could try
VBA Code:
Dim oldPrinter As String
oldPrinter = Application.ActivePrinter

' your print to pdf code goes here

Application.ActivePrinter = oldPrinter
 
Upvote 0
Thanks, I have already tried that, it seems logical, but it doesn't seem to work.
 
Upvote 0
Strange, it works fine for me. I've done a quick edit to your code, see if anything happens.
VBA Code:
Sub test()
Dim oldPrinter As String
oldPrinter = Application.ActivePrinter

Application.ScreenUpdating = False
Application.ActivePrinter = "Microsoft print to pdf"
'copy details & save to PDF file
Range("$A$1:$H$45").Select
ThisFile = Range("N5").Value & " " & Range("O5").Value
FileDrive = "C:\TMES Email Renewals\"
Selection.PrintOut Copies:=1, Collate:=True, _
printtofile:=True, prtofilename:=FileDrive & ThisFile & ".pdf"
Sheets("PDF Membership Card").Range("S1").Select

Application.ActivePrinter = oldPrinter

End Sub
 
Upvote 0
When I try it I get an error, see attached picture
 

Attachments

  • Screenshot_2.jpg
    Screenshot_2.jpg
    50.6 KB · Views: 47
Upvote 0
I just took the printer name directly from your code, it might be that you need to add "on Ne01:" or similar at the end.

The easiest way to find the correct one will be to manually select pdf as default printer, then step through the first couple of lines of code with the f8 key. Once it has passed oldPrinter = Application.ActivePrinter you can hover your cursor over that to get the correct path.

edit:- should add that you will need to manually set the printer back to default before testing the code otherwise pdf will be the default and it will still go nowhere.
 
Upvote 0
The only way I can get it to work is with the CreateObject("WScript.Network").SetDefaultPrinter "Microsoft Print to PDF line, not the Application.ActivePrinter = "Microsoft print to pdf"
 
Upvote 0
Run this and tell me exactly what is in the message box (screen cap preferable to prevent typos).
VBA Code:
Sub getname()
MsgBox Application.ActivePrinter
End Sub
 
Upvote 0
Sorry, it's given the wrong printer name. My fault entirely, try this instead.
VBA Code:
Sub getname()
CreateObject("WScript.Network").SetDefaultPrinter "Microsoft Print to PDF"
MsgBox Application.ActivePrinter
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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