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
Microsoft Excel

Avatar of undefined
Last Comment
Brian Sowter

8/22/2022 - Mon
dlmille

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
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
dlmille

You need perhaps to update the range L4 - to put your path and filename that tells the app where to save the PDF?

Dave
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Brian Sowter

ASKER
Sorry.  I dont understand range L4.

Why does your tested solution not run on my computer?

Brian
ASKER CERTIFIED SOLUTION
dlmille

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Brian Sowter

ASKER
Dave
brilliant...thanks