Excel – How to print to PDF with Excel VBA

microsoft excelmicrosoft-excel-2010pdfvba

As the title suggests… I'm looking to expand my automating to include directly printing to PDF. The macro(s) I have written already formats the print area and page setup as I need it. But when I go to record my macros nothing for printing, changing printers or saving to file is captured.

Is there a way to accomplish this? I could even get by if it just required a save location prompt. The file name I would imagine could be auto-generated by grabbing existing info within the spreadsheet.

Best Answer

if you are using Excel 2010 and above, then Excel provides you with in internal add-in which allows you to save the file as PDF format.
Use the following code in your macro to save your file as PDF

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\46506090\Desktop\Book1.pdf", Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        True  

Here you can replace the path C:\Users\46506090\Desktop\Book1.pdf and file name to your liking.

Edit: If you intend to publish the entire workbook as PDF and not just the sheet you are working on just replace ActiveSheet.ExportAsFixedFormat to ActiveWorkbook.ExportAsFixedFormat

Related Question