user1283776
user1283776

Reputation: 21764

Save some pages from some Excel sheets as PDF

I use the following code to print a range of pages from a selection of sheets. I know that I could use a "print to PDF" printer to get the data to PDF. But when I print, some aspects of my Excel document don't turn out so pretty. However when I save as PDF, the document does become pretty. So I wonder if there is there any way to SAVE a range of pages from a selection of sheets AS PDF.

If IsEmpty(Cells(i, "C").Value) Then 'Printa alla sidor
    Call Sheets(Cells(i, "A").Value).PrintOut
ElseIf Cells(i, "C").Value = 0 Then 'Printa fram till viss sida
    Call Sheets(Cells(i, "A").Value).PrintOut(1, 1)
ElseIf Cells(i, "C").Value > 0 Then 'Printa fram till viss sida
    Call Sheets(Cells(i, "A").Value).PrintOut(1, Cells(i, "C").Value)
End If

Upvotes: 0

Views: 3956

Answers (1)

Kazimierz Jawor
Kazimierz Jawor

Reputation: 19067

Yes, there is a possibility to use .ExportAsFixedFormat method according to THIS MSDN information.

The most simple example of how to use this method presents the next line of code:

Selection.ExportAsFixedFormat xlTypePDF, "testFile.pdf"

where selected range is exported to PDF File called 'TestFile.pdf'

Can I export multiple selections to the same PDF file?

Yes, you can. There are two options available:

  1. to export some non-continuous ranges from one sheet- use Union() which will make new page for each range inside:

    Union(Range("A1:f10"), Range("A15:d15")).ExportAsFixedFormat _
        xlTypePDF, "testFile.pdf"
    
  2. to export ranges from different sheets- use .ExportAsFixedFormat as Workbook method which will export each printing area in the whole workbook to one PDF document. It goes in this way:

    ActiveWorkbook.ExportAsFixedFormat xlTypePDF, "testFile2.pdf"
    

For second idea check additional parameters of .ExportAsFixedFormat method to limit pages to be printed.

Upvotes: 3

Related Questions