Excel VBA Save As (Print) To PDF
Có thể bạn quan tâm
Return to VBA Code Examples
Written by
Editorial Team
Reviewed by
Steve Rynearson
Last updated on November 18, 2021This tutorial will demonstrate how to save / print to a PDF in Excel VBA.
Print to PDF
This simple procedure will print the ActiveSheet to a PDF.
Sub SimplePrintToPDF() ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="demo.pdf", Quality:=xlQualityStandard, _ IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True End SubI’ve also created a function with error handling, etc. that will print the ActiveSheet to a PDF:
Sub PrintPDF() Call Save_PDF End Sub Function Save_PDF() As Boolean ' Copies sheets into new PDF file for e-mailing Dim Thissheet As String, ThisFile As String, PathName As String Dim SvAs As String Application.ScreenUpdating = False ' Get File Save Name Thissheet = ActiveSheet.Name ThisFile = ActiveWorkbook.Name PathName = ActiveWorkbook.Path SvAs = PathName & "\" & Thissheet & ".pdf" 'Set Print Quality On Error Resume Next ActiveSheet.PageSetup.PrintQuality = 600 Err.Clear On Error GoTo 0 ' Instruct user how to send On Error GoTo RefLibError ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SvAs, Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True On Error GoTo 0 SaveOnly: MsgBox "A copy of this sheet has been successfully saved as a .pdf file: " & vbCrLf & vbCrLf & SvAs & _ "Review the .pdf document. If the document does NOT look good, adjust your printing parameters, and try again." Save_PDF = True GoTo EndMacro RefLibError: MsgBox "Unable to save as PDF. Reference library not found." Save_PDF = False EndMacro: End FunctionThe function returns TRUE or FALSE if the print to PDF was successful or not.
Save and Email PDF Function
This function will save the ActiveSheet as a PDF and (optionally) attach the PDF to an email (assuming you have Outlook installed):
Sub Test_Save_PDF() Call Send_PDF("SendEmail") End Sub Function Send_PDF(Optional action As String = "SaveOnly") As Boolean ' Copies sheets into new PDF file for e-mailing Dim Thissheet As String, ThisFile As String, PathName As String Dim SvAs As String Application.ScreenUpdating = False ' Get File Save Name Thissheet = ActiveSheet.Name ThisFile = ActiveWorkbook.Name PathName = ActiveWorkbook.Path SvAs = PathName & "\" & Thissheet & ".pdf" 'Set Print Quality On Error Resume Next ActiveSheet.PageSetup.PrintQuality = 600 Err.Clear On Error GoTo 0 ' Instruct user how to send On Error GoTo RefLibError ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SvAs, Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True On Error GoTo 0 ' Send Email If action = "SendEmail" Then On Error GoTo SaveOnly Set olApp = CreateObject("Outlook.Application") Set olEmail = olApp.CreateItem(olMailItem) With olEmail .Subject = Thissheet & ".pdf" .Attachments.Add SvAs .Display End With On Error GoTo 0 GoTo EndMacro End If SaveOnly: MsgBox "A copy of this sheet has been successfully saved as a .pdf file: " & vbCrLf & vbCrLf & SvAs & _ "Review the .pdf document. If the document does NOT look good, adjust your printing parameters, and try again." Send_PDF = True GoTo EndMacro RefLibError: MsgBox "Unable to save as PDF. Reference library not found." Send_PDF = False EndMacro: End FunctionVBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! Learn More!VBA Code Examples Add-in
Easily access all of the code examples found on our site.
Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.
(No installation required!)
Free Download
Return to VBA Code Examples
AutoMacro: VBA Add-in with Hundreds of Ready-To-Use VBA Code Examples & much more!
Learn MoreTừ khóa » Visual Basic Excel Print To Pdf
-
How To Print To PDF In Excel VBA : With Examples And Illustrations
-
Print To PDF Using Macro Button In Excel (5 Macro Variants)
-
VBA Print To PDF And Save With Automatic File Name - Stack Overflow
-
How To Print A PDF In Excel VBA? | MrExcel Message Board
-
Excel VBA Save As PDF: Step-By-Step Guide And 10 Examples
-
Excel Macro To Save Sheets As PDF - Contextures
-
VBA For Excel (365), How To Print To PDF, While Scaling To Fit All ...
-
Save Excel As PDF With VBA
-
Excel Print To PDF Vba | PDF | Visual Basic For Applications - Scribd
-
VBA Macro One Click Print To PDF | Strength Coach Tutorials
-
How To Print To PDF With Excel VBA? - Super User
-
Solved: Print To Pdf From An Excel - Autodesk Community - AutoCAD
-
Cách Chuyển File Excel Sang PDF Sử Dụng VBA - Thủ Thuật
-
Excel Vba Open And Print PDF File - PDFCOFFEE.COM