Set Windows default printer from VBA (alternative solutions are welcomed)

quanada

New Member
Joined
Oct 12, 2017
Messages
2
I am using sendkeys in a rather complicated macro to autofill 4 PDF documents based on excel source data. Because I am only able to fill, reset or print and not able to save the filled in documents, I would like to use the "Microsoft Print to PDF" printer to save the filled in PDF. I have searched the web ad nauseum and ripped off many snippets of code including activeprinter and the like... Nothing is working... I am still kicking off an actual print job to my default printer. Since I do not want actual printed output but rather the PDF, this won't work... So I need to change the default printer so that when I send a print command via sendkeys to the document, the Save As window opens and saves to the filename. Does anyone have any thoughts or code that could help me with this?

'Beginning of Print code and get current default printer
Call GetProfileStringA("Windows", "Device", "", strLPT, 254)

Result = Application.Trim(strLPT)
ResultLength = Len(Result)

Comma1 = Application.Find(",", Result, 1)
Comma2 = Application.Find(",", Result, Comma1 + 1)

' Gets printer's name
startactiveprinter = Left(Result, Comma1 - 1)

' Gets driver
Driver = Mid(Result, Comma1 + 1, Comma2 - Comma1 - 1)

' Gets last part of device line
Port = Right(Result, ResultLength - Comma2)

startactiveprinter = startactiveprinter & " on " & Port

Printers = GetPrinterFullNames()
For N = LBound(Printers) To UBound(Printers)
testprint = Printers(N)
testprint = Left(testprint, 22)
If testprint = "Microsoft Print to PDF" Then
Application.ActivePrinter = Printers(N)
'SetDefaultPrinter (Printers(N))
Exit For
End If
Next N


'reset to original default printer
Application.ActivePrinter = startactiveprinter
'SetDefaultPrinter (startactiveprinter)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try this one liner and see if it works for you :

Code:
CreateObject("WScript.Network").SetDefaultPrinter "[COLOR=#ff0000]Write Your Target Printer Name Here[/COLOR]"
 
Last edited:
Upvote 0
Try this one liner and see if it works for you :

Code:
CreateObject("WScript.Network").SetDefaultPrinter "[COLOR=#ff0000]Write Your Target Printer Name Here[/COLOR]"

I was wondering is there any reason why this could/should not work. I have used it before and it was working fine, but suddenly it won't change the printer to another one.

VBA Code:
Sub SetDefaultPrinterPDF()

Dim PrinterToUse        As String
Dim WshNetwork          As Object
    
    PrinterToUse = shStart.Range("SelPrinterName")
    
    On Error GoTo PrinterErr
    
    Set WshNetwork = CreateObject("WScript.Network")
    WshNetwork.SetDefaultPrinter PrinterToUse
    
Exit Sub
    
PrinterErr:
MsgBox "Unable to select the printer. It may not exist on your system." & vbNewLine & "Please check with admin if the printer exists: " & vbNewLine & PrinterToUse
Set WshNetwork = Nothing

End Sub

I've tried running this code in a blank Excel file, but i still got nothing.
Also i've tried running the one liner without creating object variable, but it's still not working.
Any ideas?
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,172
Members
449,071
Latest member
cdnMech

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