Excel VBA Save File as PDF Document

The following post provides an example of how to save an Excel file as a PDF Document, using VBA code.  The macro first prompts you to enter a file name, then opens a second dialog window to pick the destination folder to save the PDF Document.  There is also an option to determine which tabs of the Excel file you would like to include the in the PDF Document.  This can be useful when you have a specific set of tabs to export and save as PDF.

 

Setup – Excel VBA Save File as PDF Document

Open an Excel file and insert a new VBA Module.  Then add the following code:

In the Excel File, make sure you setup three Tabs, including readable information on each.

In my example, I have the following:

Excel VBA Save File as PDF Document

Next, run the macro “SaveAsPDF”

Excel VBA Save File as PDF Document

You will be prompted to enter a file name:

Excel VBA Save File as PDF Document

Next, you will be prompted to pick a destination folder to save the PDF Document:

Excel VBA Save File as PDF Document

Once complete, the macro will then open the newly created PDF Document!

Note that each Tab in the Excel file is built into a separate page of the PDF Document.

Excel VBA Save File as PDF Document

If you only want to create a PDF Document with a specific range of tabs, simple add the following to your macro code:

In the “ActiveWorkbook.ExportAsFixedFormat”, add the “From” and “To” arguments.  This will determine which range of Tabs (Pages) will be built into the PDF Document.

In modified code above, I simple indicated to only build Tab 2 (Sheet2) into the PDF, resulting in the following PDF Document:

Excel VBA Save File as PDF Document

I hope you find this post useful!

 

Example File:

Excel VBA Save File as PDF Document

 

 

Additional Content

Check out more examples by visiting my Home Page

Here you will find topics covering  Qlikview  SQL Server  Excel VBA

 

 

 

Leave a Comment