[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Excel Save Worksheet as PDF using given name and path

Posted on 2014-09-01
4
Medium Priority
?
318 Views
Last Modified: 2014-09-01
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

0
Comment
Question by:dabug80
  • 2
  • 2
4 Comments
 
LVL 54

Expert Comment

by:Rgonzo1971
ID: 40296168
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
 
LVL 1

Author Comment

by:dabug80
ID: 40296201
PDFName & POPath are cell names.
0
 
LVL 54

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 40296214
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
 
LVL 1

Author Closing Comment

by:dabug80
ID: 40296234
Excellent. Works like a charm. Thanks for helping me debug, you're a champ.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question