That function will work
Put it in a standard module.
'Written: November 28, 2009
'Author: Leith Ross
'Summary: Finds a printer by name and returns the printer name and port number.
Function FindPrinter(ByVal PrinterName As String) As String
'This works with Windows 2000 and up
Dim Arr As Variant
Dim Device As Variant
Dim Devices As Variant
Dim Printer As String
Dim RegObj As Object
Dim RegValue As String
Const HKEY_CURRENT_USER = &H80000001
Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
RegObj.enumvalues HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Devices, Arr
For Each Device In Devices
RegObj.getstringvalue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Device, RegValue
Printer = Device & " on " & Split(RegValue, ",")(1)
If InStr(1, Printer, PrinterName, vbTextCompare) > 0 Then
FindPrinter = Printer
Exit Function
End If
Next
End Function
Then call it from your print routine, e.g.
I've highlighted the function line. Replace "Microsoft Print to PDF" with the full name of your printer as displayed in file/print
Sub ToPrint()
Dim CurrPrinter As String
Dim SetPrinter As String
CurrPrinter = ActivePrinter 'remember the current printer
SetPrinter = FindPrinter("Microsoft Print to PDF") 'call the function on the printer
If Len(SetPrinter) Then
ActivePrinter = SetPrinter 'if no error, change the active printer
Sheet4.PrintOut 'perform the print
ActivePrinter = CurrPrinter 'change the active printer back to what it was before
End If
End Sub
Bookmarks