• Print To PDF Using Microsoft's PDF/XPS Add-in

    Introduction:
    This article contains code that can be used to print worksheets or entire workbooks to a PDF file, using Microsoft's free Save as PDF or XPS add-in for Office 2007. These routines will NOT work in versions of Office prior to 2007. If you are running an earlier version of Office, and are running on Windows XP, then have a look at my PDF Creator articles.

    Versions Tested:
    These routines have been tested successfully using the following versions of the software:
    Operating Systems:
    • Windows Vista Ultimate 64 bit edition. (Should work fine in 32 bit edition as well.)
    Excel versions tested include:
    • Excel 2007
    Code Samples
    The first code sample will print the active sheet(s) to a single PDF file. (To select multiple sheets, hold down the CTRL key and click each sheet tab. Running this code will then print all selected sheets into a single PDF file.)

    NOTE:
    In any of the routines below, to have Adobe Acrobat automatically open after printing to file, change the value of "OpenAfterPublish" to True. This is a handy way to review the output of the PDF, allowing you to make sure it was created correctly.

    Code:
    Sub SaveToPDF()
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: Print active sheet(s) to PDF file using Microsoft's 2007 Add-in
        With ActiveSheet
            .ExportAsFixedFormat _
                    Type:=xlTypePDF, _
                    Filename:="C:\Test\Test.pdf", _
                    OpenAfterPublish:=False
        End With
    End Sub
    The next sample uses code to specifically print two worksheets to a single PDF file.
    Code:
    Sub SaveSpecificToPDF()
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: Print specific sheets to PDF file using Microsoft's 2007 Add-in
        Sheets(Array("Sheet1", "Sheet3")).Select
        With ActiveSheet
            .ExportAsFixedFormat _
                    Type:=xlTypePDF, _
                    Filename:="C:\Test\Test.pdf", _
                    OpenAfterPublish:=False
        End With
    End Sub
    This code sample will save the entire workbook to a PDF.
    Code:
    Sub SaveFileToPDF()
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: Print entire workbook to PDF file using Microsoft's 2007 Add-in
        ActiveWorkbook.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:="C:\Test\Test.pdf", _
                OpenAfterPublish:=False
    End Sub
    This sample will prompt the user for a location to save the active worksheet as a PDF.
    Code:
    Sub SaveFileToPDF_Prompt()
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: Save the file as a PDF to a location provided by the user at runtime
    '               using Microsoft's 2007 Add-in
        Dim vPDFPath As Variant
        
        Do
            'Collect output file name and test if valid
            bRestart = False
            vPDFPath = Application.GetSaveAsFilename(, "PDF Files (*.pdf), *.pdf")
    
            'Check if user cancelled
            If CStr(vPDFPath) = "False" Then
                Exit Sub
            Else
                lAppSep = InStrRev(vPDFPath, Application.PathSeparator)
            End If
    
            'Check if file exists, as PDFCreator will not overwrite
            If UCase(Dir(vPDFPath)) = UCase(Right(vPDFPath, Len(vPDFPath) - lAppSep)) Then
                Select Case MsgBox("File already exists.  Would you like to overwrite it?", _
                                   vbYesNoCancel, "Destination file exists!")
                    Case vbYes
                        Kill vPDFPath
                    Case vbNo
                        bRestart = True
                    Case vbCancel
                        Exit Sub
                End Select
            End If
        Loop Until bRestart = False
    
        'Save the file as a PDF
        ActiveWorkbook.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=vPDFPath, _
                OpenAfterPublish:=False
    End Sub
    More Examples
    For more examples on using Microsoft's PDF add-in, visit Ron deBruin's PDF page.
  • MVP Logo
  •  Donations

    If you like our website and would like to give something in return, you can make a donation. All donations are gratefully received and go to support the site.


    Select your preferred currency and donation amount, then click the donate button.

  • Recent Forum Posts

    Colo

    How to Rename a File from English to Other Language?

    Yeah, some massive HTML conversion is my line. In other words, most of difficult things can be done with Excel alone like this time. Well done, Excel!...

    Colo Today, 07:36 AM Go to last post
    Ken Puls

    Link a series name to a cell using Excel 2010 VBA

    Hi there,

    I recorded linking the title to a cell and it came back with the following. Does this help?

    Code:
        ActiveChart.SetElement
    ...

    Ken Puls Today, 04:06 AM Go to last post
    Ken Puls

    How to Rename a File from English to Other Language?

    Colo, that was way too easy... I was expecting some massive HTML conversion, or a huge engine to compare each character against a library of Chr codes!...

    Ken Puls Today, 04:02 AM Go to last post
    Ken Puls

    Shared file\macro & IP address

    Oh, and as for the max number of users who can access the file in the shared folder at once...

    • For reading, I believe it's unlimited. (The second and
    ...

    Ken Puls Today, 03:59 AM Go to last post
    Ken Puls

    Shared file\macro & IP address

    ibrahimaa,

    There is no one-line way to get your IP address the way you are getting the username. So you're going to need more code than...

    Ken Puls Today, 03:56 AM Go to last post