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-24T16:14:56+00:00

    Hey,

    VBA code snippet that you can use to print an Excel worksheet to a PDF file. Make sure you adjust the code according to your specific needs:

    vbaCopy code Sub PrintToPDF() Dim ws As Worksheet Dim pdfPath As String ' Set the worksheet you want to print Set ws = ThisWorkbook.Sheets("Sheet1") ' Replace "Sheet1" with your sheet's name ' Set the PDF file path and name pdfPath = "C:\Path\To\Your\File.pdf" ' Replace with your desired file path ' Print the worksheet to PDF ws. ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard End Sub Instructions for using the code: 1. Open the Excel workbook you want to work with. 2. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. 3. Click Insert > Module to insert a new module. 4. Copy and paste the provided code into the module. 5. Modify the code: • Set the ws variable to the worksheet you want to print. • Set the pdfPath variable to the desired file path and name for the PDF. 6. Close the VBA editor. 7. Press Alt + F8 to open the "Macro" dialog box. Select "PrintToPDF" (or whatever you named your macro) from the list and click "Run."

    If you have any further questions or need additional assistance, please feel free to ask. I'm here to help!

    If the reply was helpful, please don’t forget to upvote or accept it as an answer, Thank you

    Kind regards

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2023-08-26T01:55:44+00:00

    Hi,

    I have workbook and not worksheet here.

    How to write that code?

    I have a workbook with 3 worksheets, So I have PRINT button which prints all these 3 sheets here.

    How to write the code for that ?

    PRoblem is when i am runnignthe code, it is printing it with Company printer and not MS PRint to PDF here

    0 comments No comments
  3. Anonymous
    2023-08-26T07:46:41+00:00

    Hello,

    To print all the worksheets in a workbook, you can use the following VBA code:

    Sub PrintAllSheets() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets ws. PrintOut Next ws End Sub

    This code will print all the worksheets in the active workbook. If you want to print only specific worksheets, you can modify the code accordingly. Regarding your issue with printing to PDF, you can try changing the default printer to Microsoft Print to PDF before running the code. To do this, follow these steps:

    1. Open the Control Panel.
    2. Click on Devices and Printers.
    3. Right-click on Microsoft Print to PDF and select Set as the default printer. After setting Microsoft Print to PDF as your default printer, run the code again and see if it prints to PDF.

    If you have any further questions or need additional assistance, please feel free to ask. I'm here to help!

    If the reply was helpful, please don’t forget to upvote or accept it as an answer, Thank you

    Kind regards

    0 comments No comments