How can I use VBA to print to PDF, with an example? 2

How can I use VBA to print to PDF, with an example?

VBA (Visual Basic for Applications) is a programming language used to automate tasks in Microsoft Office applications. With VBA, it is possible to print documents to PDF format, which is a popular and widely used file format for sharing and archiving documents. This can be done by using the “Print to PDF” function in VBA, which allows users to specify the desired output file name and location. For example, a VBA code can be written to print a selected range of cells in an Excel spreadsheet to a PDF file, which can then be saved in a specified folder. This feature of VBA provides a convenient and efficient way to generate PDF documents from within Microsoft Office applications.

Print to PDF Using VBA (With Example)


You can use the following syntax in VBA to print the currently active Excel sheet to a PDF:

Sub PrintToPDF()

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                                Filename:="my_data.pdf", _
                                Quality:=xlQualityStandard, _
                                IncludeDocProperties:=False, _
                                IgnorePrintAreas:=False, _                                OpenAfterPublish:=TrueEnd Sub

This particular macro will print the currently active Excel sheet to a PDF called my_data.pdf and it will be saved in the current folder.

Note #1: You can also include a full file path in the Filename argument to save the PDF to a specific folder.

Note #2: The line OpenAfterPublish:= True tells VBA to open the PDF as soon as it is exported. You can leave out this argument if you don’t want the PDF to be opened after exporting.

Note #3: The only required argument in the ExportAsFixedFormat method is Type, which must be set to xlTypePDF to print the sheet to a PDF format.

The following example shows how to use this macro in practice.

Example: Export Excel Sheet to PDF Using VBA

Suppose we have the following Excel sheet that contains information about various basketball players:

Now suppose that we would like to export this sheet to a PDF called my_data.pdf.

We can create the following macro to do so:

Sub PrintToPDF()

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                                Filename:="my_data.pdf", _
                                Quality:=xlQualityStandard, _
                                IncludeDocProperties:=False, _
                                IgnorePrintAreas:=False, _                                OpenAfterPublish:=TrueEnd Sub

When we run this macro, the Excel sheet is exported to a PDF and then the PDF is automatically opened:

Note that the exact formatting of the cells with the borders and the fill color is included in the PDF.

Note: You can find the complete documentation for the ExportAsFixedFormat method in VBA .

Cite this article

stats writer (2024). How can I use VBA to print to PDF, with an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-print-to-pdf-with-an-example/

stats writer. "How can I use VBA to print to PDF, with an example?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-print-to-pdf-with-an-example/.

stats writer. "How can I use VBA to print to PDF, with an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-print-to-pdf-with-an-example/.

stats writer (2024) 'How can I use VBA to print to PDF, with an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-print-to-pdf-with-an-example/.

[1] stats writer, "How can I use VBA to print to PDF, with an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use VBA to print to PDF, with an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top