Sub topic 10
Exporting a Sheet, Selection, or Workbook to PDF
This chapter assumes you can set a print area, choose orientation, apply scaling, and set print titles and footers from the earlier chapters. PDF export reuses all of those settings, so the work you did there carries over.
How export relates to printing
A PDF export runs the same pagination as a printer. Excel applies the print area, orientation, scaling, page breaks, print titles, and headers and footers, then writes the pages to a file instead of a device. If the preview looks right, the PDF will match it.
Excel's PDF output depends on the paper size and margins you chose, and on the selected printer driver. Check Print Preview before you export.
Exporting from the ribbon
- Open the sheet you want and check it in Print Preview.
- Choose File > Export > Create PDF/XPS Document, then Create PDF/XPS. You can also use File > Save As and set Save as type to PDF.
- Pick a folder and file name.
- Click Options to choose what to export.
- Click Publish (or Save).
Choosing what to export
The Options dialog controls the scope. Pick the option that matches your goal.
| Option | What it exports |
|---|---|
| Active sheet(s) | Only the sheets currently selected. This is the default. |
| Entire workbook | Every visible sheet, in tab order. |
| Selection | Only the cells you highlighted before opening the dialog. |
| Page(s) From/To | A range of pages from the output. |
Two related settings sit in the same dialog:
- Ignore print areas exports every used cell and skips the print areas you defined. Leave it cleared if you want the print area respected.
- Optimize for has two choices. Standard gives better quality for printing. Minimum size gives a smaller file for email or web use.
Exporting several sheets
If you select more than one sheet tab, Active sheet(s) exports all of them into one PDF. Hold Ctrl and click each tab to select a group. Each sheet keeps its own page setup. Page numbers, however, usually restart at 1 for each sheet unless you set First page number in Page Setup.
The export process
Exporting with VBA
The ExportAsFixedFormat method does the same job in code. This macro saves the active sheet next to the workbook and respects the print area and page setup already defined.
Sub ExportSheetToPdf()
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "\Report.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End SubKey arguments:
Type:=xlTypePDFselects the PDF format.QualityacceptsxlQualityStandardorxlQualityMinimum.IgnorePrintAreas:=Falsekeeps your print area in effect.OpenAfterPublish:=Falsestops Excel from opening the file when it finishes.
To export the whole workbook, call the method on ThisWorkbook instead of ActiveSheet. To export a selection, call it on Selection.
The file path uses a backslash, so this example is written for Windows. Also, ThisWorkbook.Path is empty if the workbook has never been saved, so save it first.
Common errors
- Content is cut off or split across pages. Fix the scaling or page breaks first, then export again.
- A PDF has blank pages. The print area or used range probably includes empty cells. Clear and reset the print area.
- Text looks tiny. Fit to 1 page wide is shrinking a wide sheet. Switch to landscape or reduce the number of columns.
- The file is very large. Use Minimum size, or compress any images in the sheet.
- Export fails in VBA. Check that the folder exists, the file is not open in a PDF reader, and the workbook has been saved.
Exercise
You select cells A1:F20 and want a PDF that contains only those cells. You also have a print area defined on the sheet. Which options do you choose in the Options dialog, and why?
Show answer
Choose Selection under Publish what, and leave Ignore print areas cleared so Excel does not switch to exporting every used cell. Selection limits the output to A1:F20, and the existing page setup (orientation, scaling, titles, footers) still applies.
Exercise
A macro that calls ExportAsFixedFormat with ThisWorkbook.Path fails on a new workbook. What is the likely cause, and how do you fix it?
Show answer
A workbook that has never been saved has an empty Path, so the file name becomes invalid. Save the workbook to a folder first, then run the macro again.