Link to home
Start Free TrialLog in
Avatar of Brian Sowter
Brian Sowter

asked on

Excel VBA To Print range To PDF And Save Using Name In Cell

I am looking for sample code to print a defined range in an Excel spreadsheet to a PDF and save it using name in a cell.

Any help appreciated
Avatar of dlmille
dlmille
Flag of United States of America image

Assuming you have Excel 2007+, and in the worksheet you've defined the pdf print range (PDFRange) and file to print to (pdfPathFile), you can use this code.

Sub printPDFSave()
Dim rng As Range
Dim fPathFile As String

    Set rng = [PDFRange]
    fPathFile = [pdfPathFile]
    rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPathFile, quality:=xlQualityStandard, _
        includedocproperties:=True, ignoreprintareas:=True, openafterpublish:=True
    
    
End Sub

Open in new window


See attached.

Dave
printRngAsPDF-r1.xlsm
Avatar of Brian Sowter
Brian Sowter

ASKER

Hello Dave
Thank you very much for this.  I get
 
"Run time error 1004.  The document was not saved ...may be open or an error was encountered."

when I click the button.  I have Excel 2007.
Regards
Brian
You need perhaps to update the range L4 - to put your path and filename that tells the app where to save the PDF?

Dave
Sorry.  I dont understand range L4.

Why does your tested solution not run on my computer?

Brian
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dave
brilliant...thanks