Create a PDF in Excel – Excel to PDF Options
Contents [show]
Overview
In this article I will be demonstrating some of the various ways to create a PDF file from a range in Microsoft Excel. I
will show you how to store it in any location on your computer. We will be recording a macro to help is get the code
and then I’ll show you how to add cell references and the current date and time and many other features to make the
VBA code involved very flexible. Watch the video below first so that you can get an overview of what we are
accomplishing here. Second download the template so that your experience in working with the code below will be
much easier.
Video Create PDF in Excel
Download the Template
Template Excel to PDF: Create from range
Office 2007 Addin
If you are using Office 2007 then you will need to have the PDF addin installed and activated.
Here a link with additional information.
http://www.microsoft.com/en-us/download/details.aspx?id=9943
Record a macro to create a PDF in Excel
From the Developer tab record a Macro create the PDF in Excel
Select the area on the worksheet
Developer Tab / Record Macro
File / SaveAs
Choose a location in the left panel
Add a file name
In Save As type choose PDF
We will return later to edit this code.
Using ranges for the data
Even though we have IgnorePrintArea set to False. Which means it will print the print area it is necessary to set a
range.
If your range is not changing then you can refer to it explicitly or use a static named range.
Here are our options
Cell references
Static named range
Dynamic named range
Create a named range with code
Dynamic named range tutorial
Static named range tutorial
Set print area and page breaks
You will need to set the print area and page breaks for the file.
This is how you set the print area manually.
Highlight the area
On the Ribbon choose Page Layout
Print area
Set Print area
I will demonstrate how to do this with code in a moment.
To set the page breaks
On the Ribbon in the View tab
Choose Page Breaks
Adjust to suit by going to the Page Layout tab and choose Page Setup / Page and set your Orientation and % of
Normal to fill the page.
Modifying our recorded Macro: Various File Paths
Recorded Macro
Here is our recorded macro. Let’s see if we can make some changes to make it a little bit more flexible.
Sub Macro1()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\Trevor\Desktop\MyPDF.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
Direct file path
In this example we have taken the code above and assigned the path as a variable.
Dpath = "C:\Users\Trevor\Desktop\MyPDF" & " " & Format(Now, "yyyy-mm-dd hh-mm")
Sub Direct()
Dim Dpath As String
Dpath = "C:\Users\Trevor\Desktop\MyPDF" & " " & Format(Now, "yyyy-mm-dd hh-mm")
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Dpath, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
Direct path plus cell reference and date and time
Let’s look at a few more changes that we could make.
In this procedure we:
Assign variables for a sheet reference
Assign variable for the file path
Create a named range to adjust to the data
Set a print area for the name
Create the PDF
Sub PrintPDFAllPath()
Dim Dpath As String
Dim FileN As Range, MyRange As Range
'turn off screen updating
Application.ScreenUpdating = False
Set FileN = Sheet1.Range("A1")
'Dpath = "C:\Users\Trevor\Desktop\" & FileN 'change your path do not forget the last backslash
Dpath = "C:\Users\Trevor\Desktop\" & FileN & " " & Format(Now, "yyyy-mm-dd hh-mm") 'change your path do not
forget the last backslash
'set the named range for the PDF
With Sheet1
.Range("C6:K" & Cells(Rows.Count, "C").End(xlUp).Row).Name = "PDFRng"
End With
'set range
Set MyRange = Sheet1.Range("PDFRng")
Sheet1.PageSetup.PrintArea = "PDFRng"
'create the PDF
On Error Resume Next
MyRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Dpath, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
'error handler
On Error GoTo 0
'clear the page breaks
ActiveSheet.DisplayPageBreaks = False
Application.ScreenUpdating = False
End Sub
Open Dialog to get the file path
This is my one of 2 preferred options that I would use if I was going to send the file to another computer
Sub PrintPDFAll()
'turn off screen updating
Dim Opendialog
Dim MyRange As Range
Application.ScreenUpdating = False
'open dialog and set file type
Opendialog = Application.GetSaveAsFilename("", filefilter:="PDF Files (*.pdf), *.pdf", _
Title:="Your Invoice")
'if no value is added for file name
If Opendialog = False Then
MsgBox "The operation was not successful"
Exit Sub
End If
'set the named range for the PDF
With Sheet1
.Range("C6:K" & Cells(Rows.Count, "C").End(xlUp).Row).Name = "PDFRng"
End With
'set range
Set MyRange = Sheet1.Range("PDFRng")
Sheet1.PageSetup.PrintArea = "PDFRng"
'create the PDF
On Error Resume Next
MyRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Opendialog, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
'error handler
On Error GoTo 0
'clear the page breaks
ActiveSheet.DisplayPageBreaks = False
Application.ScreenUpdating = False
End Sub
Folder Path Macro (assign to a shape)
This is the procedure to call the file dialog box. When the folder is selected the path and a backslash is added to the
sheet.
Sub FolderSelect()
Dim MyFolder As FileDialog
Dim diaFolder As Variant
On Error GoTo errHandler:
' Open the file dialog
Set MyFolder = Application.FileDialog(msoFileDialogFolderPicker)
MyFolder.AllowMultiSelect = False
MyFolder.Show
'MsgBox diaFolder.SelectedItems(1)
Sheet1.Range("B2") = MyFolder.SelectedItems(1) & "\"
Set diaFolder = Nothing
On Error GoTo 0
Exit Sub
'error block
errHandler:
MsgBox "An Error has Occurred " & vbCrLf & _
"The error number is: " & Err.Number & vbCrLf & _
Err.Description & vbCrLf & "Please notify the administrator"
End Sub
Folder Path and Sheet Reference
In this procedure we are allowing the user to select the folder path. A backslash and the file name from a sheet
reference is then added to the file path. This also is an excellent way to send the information on to others as it has
built in flexibility.
Sub PrintPDFAllPathFolder()
Dim Dpath As String
Dim FileN As Range, FolderPath As Range
Dim MyRange As Range
'turn off screen updating
Application.ScreenUpdating = False
Set FileN = Sheet1.Range("A1")
Set FolderPath = Sheet1.Range("B2")
Dpath = FolderPath & FileN & " " & Format(Now, "yyyy-mm-dd hh-mm") 'change your path do not forget the last
backslash
'set the named range for the PDF
With Sheet1
.Range("C6:K" & Cells(Rows.Count, "C").End(xlUp).Row).Name = "PDFRng"
End With
'set range
Set MyRange = Sheet1.Range("PDFRng")
Sheet1.PageSetup.PrintArea = "PDFRng"
'create the PDF
On Error Resume Next
MyRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Dpath, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
'error handler
On Error GoTo 0
'clear the page breaks
ActiveSheet.DisplayPageBreaks = False
Application.ScreenUpdating = False
End Sub
Conclusion
There are an endless ways to set file paths that will save your PDF to various locations on your computer. I hope that
the methods used above will help you and get you started in understanding the basics that are involved. this is a
great feature that has been included since Microsoft Excel 2007 onwards. It is often overlooked, so why not put it to
good use in your awesome applications