Excel Save Worksheet as PDF using given name and path

Hello,

I have a single Excel worksheet (PO Template). I am trying to code a macro to save the sheet (PO Template) as a PDF to a given filename (PDFName) and a given file location (POPath).
 
PDFName = test
POPath = S:\test\again\


Problem 1:
The macro triggers a message box, but the (PDFName) reference is not part of the message.

It says: Are you sure you want to save this PO Requisition as .pdf?
It should say: Are you sure you want to save this PO Requisition as (PDFname).pdf?


Problem 2:
Currently when I run the macro, no errors are triggered, but nothing happens. The Excel sheet isn't being saved as a PDF to the file location


Below is the macro:


Sub mac_PORequestPDF()
'
' mac_PORequestPDF Macro
'
Sheets("PO Template").Select

    Dim ThisFile As String
    
    ThisFile = Sheets("PO Template").Range("POPath").Value

    If MsgBox("Are you sure you want to save this PO Requisition as " & (PDFName) & ".pdf?", vbYesNo) = vbYes Then
                Sheets(Array("PO Template")).Copy ' Create a new workbook with this sheets
                ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFName, Quality:=xlQualityStandard, _
                                        IncludeDocProperties:=True, OpenAfterPublish:=False
        ActiveWorkbook.Close SaveChanges:=False
    
    
    Sheets("PO Template").Select
    
    Else
        Sheets("PO Template").Activate
    End If
'
End Sub

Open in new window

LVL 1
dabug80Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

In your example PDFName is not defined

Sub mac_PORequestPDF()
'
' mac_PORequestPDF Macro
'
Sheets("PO Template").Select

    Dim ThisFile As String
   
    ThisFile = Sheets("PO Template").Range("POPath").Value

    PDFName = "test"
    POPath = "S:\test\again\"

    If MsgBox("Are you sure you want to save this PO Requisition as " &  PDFName & ".pdf?", vbYesNo) = vbYes Then
                Sheets(Array("PO Template")).Copy ' Create a new workbook with this sheets
                ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=POPath & PDFName & ".pdf" , Quality:=xlQualityStandard, _
                                        IncludeDocProperties:=True, OpenAfterPublish:=False
        ActiveWorkbook.Close SaveChanges:=False
   
   
    Sheets("PO Template").Select
   
    Else
        Sheets("PO Template").Activate
    End If
'
End Sub
0
dabug80Author Commented:
PDFName & POPath are cell names.
0
Rgonzo1971Commented:
Let's try

Sub mac_PORequestPDF()
'
' mac_PORequestPDF Macro
'
Sheets("PO Template").Select

    Dim ThisFile As String
    
    ThisPath = Sheets("PO Template").Range("POPath").Value
    ThisFile = Sheets("PO Template").Range("PDFName").Value & ".pdf"


    If MsgBox("Are you sure you want to save this PO Requisition as " & ThisFile & "?", vbYesNo) = vbYes Then
                Sheets(Array("PO Template")).Copy ' Create a new workbook with this sheets
                ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisPath & ThisFile, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, OpenAfterPublish:=False
        ActiveWorkbook.Close SaveChanges:=False
    
    
    Sheets("PO Template").Select
    
    Else
        Sheets("PO Template").Activate
    End If
'
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dabug80Author Commented:
Excellent. Works like a charm. Thanks for helping me debug, you're a champ.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.