Save Excel Range as PDF using VBA

To save a selected range in excel (say a receipt) as PDF in Excel using VBA use below syntax.

The Generic Code

Sub SaveRangeAsPDF()
        Range.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\file_name", Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub

The above code is a generic code to save a fixed range as a PDF document. When your record macro while you export excel to PDF, a similar code will appear.

Range: It can be a fixed range, a dynamic range or a selection. You need to define it.

Filename: This is the fully qualified name of pdf. Define it as a string.

The rest of the variables can be left as it is. I have explained them after example.

Example: Print Excel receipt as a PDF using VBA

Let's say you have to print and send receipts in Excel on a regular basis. In that case, you would like to have it automated. You wouldn't want to do the export excel to pdf over and over again. If you can just use a button to do this it would be helpful, isn't it?

Here, I have designed this receipt format. I want to print/save/export it as a pdf using the button "Create PDF".

 This receipt covers the range "A2:L21". I have already set the print area.

To save on this excel range as PDF, we will use the above mentioned generic VBA code of exporting excel sheet to pdf. We will adjust the code as per our requirement.

Sub PrintSelectionToPDF()
Dim invoiceRng As Range
Dim pdfile As String
'Setting range to be printed
Set invoiceRng = Range("A1:L21")
'setting file name with a time stamp.
pdfile = "invoice" & "_" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf"
'setting the fulli qualified name. The resultent pdf will be saved where the main file exists.
pdfile = ThisWorkbook.Path & strfile
invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:= pdfile, _
Quality:= xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
End Sub

Using The Code:

Copy the above code in your receipt file by opening the VBA editor ( use Alt+F11). Adjust the range and the file path (if you want to) in the code that you want to print. You can download the working file below.

image 48Save Excel Range as PDF using VBA


Dim invoiceRng As Range
Dim pdfile As String

The code is simple. First, we have created two variables. "InvoiceRng as Range" for the range/sheet that we want to save as pdf. strFile for the fully qualified filename of the resultant PDF.

Set invoiceRng = Range("A1:L21")

Since our printing range is fixed, we set the invoice range as Range("A1:L21").

pdfile = "invoice" & "_" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf"
pdfile = ThisWorkbook.Path & strfile

In the above two lines, we first name the file with a timestamp and then add to the path of the main file. As a result, pdfile contains the fully qualified name of the resultant pdf file.

invoiceRng.ExportAsFixedFormat _
Type:=xlTypePDF, _ 
Filename:= pdfile, _ 
Quality:= xlQualityStandard, _ 
IncludeDocProperties:=True, _ 
IgnorePrintAreas:=True, _ 

Finally, we use the ExpoortAsFixedFormat method of  Range class to print the defined Excel range as PDF. We define type as xlTypePDF. Another choice is xlTypeXPS, which will save the selected range as XPS file.

We set the Filename as pdfile, which contains the string of the fully qualified name of the pdf file. You can write a hardcoded text here or customize it as per your need.

Next, we set the quality of the pdfile as xlQualityStandard. We have another choice as xlQualityMinimum.

Next, we set the IncludeDocProperties as True. It means that the resultant PDF will have the properties of a Document.

Then we set the IgnorePrintAreas as True. It means it will ignore any print area already set.

Finally, we set OpenAfterPublish as False. It means that the file you create will not open automatically. I set it false because I create 100s of files using a loop, and I don't want them to open. If you want to open the file after creating using excel, set it True.

Usage of printing selected range as pdf

One basic use is to print the selected range by just clicking on one button. But the best use is to use it in a loop where you need to create multiple receipts for different clients. Create a sub for filling data and then use this code to print the selected range in a loop.

So yeah guys, this how you can save selected range as pdf. I hope it was useful for you. If you still have any doubt or query regarding print in excel using VBA, ask in the comments section below.

Related Articles:

Print multiple selections on one sheet using VBA in Microsoft Excel | Select multiple ranges and print each range in a different sheet using VBA.

Print all workbooks in a folder using VBA in Microsoft Excel | Use this VBA code to print and save each excel workbook in a folder using.

Popular Articles:

Split Excel Sheet Into Multiple Files Based On Column Using VBA | This VBA code split excel sheet base on unique values in a specified column. Download the working file.

Turn Off Warning Messages Using VBA In Microsoft Excel 2016 | To turn off warning messages that interrupt the running VBA code, we use the Application class.

Add And Save New Workbook Using VBA In Microsoft Excel 2016 | To add and save workbooks using VBA we use Workbooks class. Workbooks.Add adds new workbook easily, however...





Users are saying about us...

  1. Anders Jørgensen

    How would you go about changing between landscape/portrait mode when printing to pdf?

    Great code btw, as a complete novice I could import it and customize it to me needs!

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.

Visit Us On TwitterVisit Us On FacebookVisit Us On Youtube