Microsoft Print to PDF using VBA Code

Anonymous
2023-08-22T15:25:18+00:00

how to write VBA Code for Ensuring when print is done, it is done automatically using Microsoft Print to PDF using VBA Code in the macro called as "RUN PRINT"

Please share the code

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2023-08-26T18:28:07+00:00

    Hello

    Here is a VBA code to set the default printer to Microsoft Print to PDF:

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

    Kind regards

    5 people found this answer helpful.
    0 comments No comments
Answer accepted by question author
  1. Anonymous
    2023-08-27T07:53:13+00:00

    Hello,

    If you run this macro, it will change the default printer to Microsoft Print to PDF, regardless of the current default printer.

    Kind Regards

    2 people found this answer helpful.
    0 comments No comments

14 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-08-31T11:06:15+00:00

    Hi Sir,

    I used above command in my code.

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

    But it is not changing default printer from company printer to MS PRint to PDF here

    How to do it otherwise now ?

    0 comments No comments
  2. Anonymous
    2023-12-09T19:57:06+00:00

    The code to do what you actually want to do here is below. The issue you are running into is that the printer is not named properly.

    1. Copy the code below and comment out the items from 'print statements to 'end print statements.
    2. Remove the comment in front of 'MsgBox Application.ActivePrinter
    3. Set your printer to PDF and then run the macro
    4. Take note of the printer name. It will look something like what is in the code.
    5. Change your printer to your default printer
    6. Run the macro again.
    7. Take note of the printer name.
    8. Change the printer names in quotes to match up with what you captured above.
    9. Comment out the MsgBox line.
    10. Remove the comments in front of the lines from 'print statements to 'end print statements

    You are ready to go. I've tested this on my own machine.

    Note: If you kick off the pdf it will open up a Save File dialog. If you cancel out of that, it will return a 1004 error. The error handler routine will handle that cleanly and simply close the dialog. I've also included a couple of handy parameters (from, to) that you can include if those are helpful

    Good luck. Code below


    Sub Macro1()

    On Error GoTo errorhandler

    'MsgBox Application.ActivePrinter 
    

    'print statements

    Application.ActivePrinter = "Microsoft Print to PDF on Ne01:" 
    
    ActiveWindow.ActiveSheet.PrintOut from:=1, to:=1, IgnorePrintAreas:=False 
    
    Application.ActivePrinter = "Canon MF650C Series UFR II on Ne03:" 
    

    'end print statements

    errorhandler:

    If Err.Number = 1004 Then

    Exit Sub 
    

    Else

    MsgBox "Error=" & Err.Number 
    

    End If

    End Sub

    0 comments No comments