How to save a spreadsheet that was converted to a pdf to a file location

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
The goal I am trying to achieve is to convert a spreadsheet to a pdf, save the pdf to a specified folder, and email that pdf all with a click of a button. However I am stuck on the second part, saving the new converted pdf to a specified file location. Here's what I have so far, everything works great until the last line of code.
Code:
ActiveWorkbook.SaveAs filename:=sPath
I get Runtime error '1004': Application defined or object-defined error.
VBA Code:
Private Sub btnSave_Email_Click()
    Dim sPath As String
    Dim sFilename As String
    
    sPath = "C:\Users\UserName\Desktop\Saved PDF TEST\"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF
    ActiveWorkbook.SaveAs filename:=sPath
    
End Sub

Thank You
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
What do you want to call the file?
 
Upvote 0
You haven't assigned a file name.

VBA Code:
Sub btnSave_Email_Click()
    Dim sPath As String
    Dim sFilename As String
    
    sPath = "C:\Users\UserName\Desktop\Saved PDF TEST\myPDFFile.pdf" 'Assign the file name
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sPath
    
End Sub
 
Upvote 0
I'm sorry. I can't believe I missed that. I must of gotten ahead of myself. The excel filename will stay the same and converted PDF name will be the same as the excel filename. So I added this line of code to define the filename and inserted it after the "sPath = ...". Here's the updated code.

Code:
Private Sub btnSave_Email_Click()
    Dim sPath As String
    Dim sFilename As String
    
    sPath = "C:\Users\Jacob B Cutler\Desktop\Saved PDF TEST\"
    sFilename = ThisWorkbook.Name
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF
    ActiveWorkbook.SaveAs filename:=sPath
    
End Sub
VBA Code:
sFilename = ThisWorkbook.Name
 
Upvote 0
In that case use
VBA Code:
Private Sub btnSave_Email_Click()
    Dim sPath As String
    Dim sFilename As String
    
    sPath = "C:\Users\Jacob B Cutler\Desktop\Saved PDF TEST\"
    sFilename = ThisWorkbook.Name
    ActiveSheet.ExportAsFixedFormat xlTypePDF, sPath & sFilename

End Sub
 
Upvote 0
When the line of code below is ran the pdf is automatically saved as the same filename. I just need to figure out how to save that pdf to that location.
 
Upvote 0
Are you talking about my code?
If so it will save it in the specified location.
 
Upvote 0
Fluff, When I put your code in, I receive a Compile Error Syntax error. Don't I need to add a SaveAs somewhere in there?

Thank You
 
Upvote 0
Nevermind. I left the word "Type:=" in the code. I also noticed that Excel Macro is also add to the file location. I forgot to mention that the Excel File macro is a Template and does not need to be added to the file folder only the PDF. Is this possible to do?
 
Upvote 0
Please try the code I posted as-is. Without any changes or amendments.
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,904
Members
449,194
Latest member
JayEggleton

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top