Setting Default Printer to Microsoft Print to PDF

Viewed 6282

I am currently using a vba macro to fill in a pdf form and print it to another pdf. This works fine if the 'Microsoft Print to PDF' is the default printer however I do need to change this from time to time and forget to change it back. I am currently using this code below on another macro to change the default printer to an actual paper printer in the office; however, when I use this with the Microsoft Print to PDF printer it runs but does not change the default printer.

CreateObject("WScript.Network").SetDefaultPrinter "Microsoft Print to PDF"
2 Answers

You can set the active printer in excel vba as follows:

Application.ActivePrinter = "Brother HL-L2350DW series on Ne04:"

You can determine the printer name via Devices and Printers. However, the on Ne04: is a bit of a mistery. It is supposed to be the port but if you look in the printer properties and/or the registry this value is nowhere to be found.

I found the only way to determine this value is by setting a printer to default then enter Excel and enter ?ActivePrinter into the VBA Immediate window.

?ActivePrinter
Foxit Reader PDF Printer on Ne02:

Once you have the information you need it's easy to change your code as follows:

DefaultPtr =Application.ActivePrinter
Application.ActivePrinter = "Foxit Reader PDF Printer on Ne02:"
'***Do your printing here***
Application.ActivePrinter = DefalutPtr

This is an addon to @FaneDuru's solution that implements a dialogbox showing the printer list, highlighting the currently active printer and allowing you to select any other printer. It also encompasses the fix for the problem with the "Send to OneNote 2016" printer problem. The code is for running the dialog box shown below:

Select Printer Dialog

To run the dialogbox simply add: ufSelectPrinter.Show to your VBA code. Note: You'll have to create the dialog box using the labels shown in the code.

Option Explicit

Dim Printers() As String

Private Sub UserForm_Initialize()

    Dim iSelPtr As Integer
    Dim lCntr   As Long
    Dim zCurPtr As String
    
    zCurPtr = Application.ActivePrinter
    
    Printers = GetPrinterFullNames()
    
    For lCntr = LBound(Printers) To UBound(Printers)
    
      '*** Fix for: "Send to OneNote 2016" printer that
      '***          that prints to nul: port which is dropped
      '***          by GetPrinterFullNames() routine.
        If InStr(Printers(lCntr), ":") = 0 Then
          Printers(lCntr) = Printers(lCntr) & "nul:"
        End If
      '*** END of Fix ***
        
      '*** Find current printer in the list and adjust the
      '*** index for the zero based listbox.
      
      If Printers(lCntr) = zCurPtr Then iSelPtr = lCntr - 1
    
    Next lCntr
    
  '*** Populate the List Box ***
  Me.lboxSelectPrinter.List = Printers()
  
  '*** Highlight the current Active Printer ***
  Me.lboxSelectPrinter.Selected(iSelPtr) = True
  
End Sub

Private Sub CBCancel_Click()
   Unload Me
End Sub

Private Sub CBOk_Click()

   Dim iSelected As Integer
   iSelected = Me.lboxSelectPrinter.ListIndex + 1 '***Zero Based***
'   Debug.Print Printers(iSelected)
   Application.ActivePrinter = Printers(iSelected)
   Unload Me
   
End Sub

HTH

Related