Saving Workbook as .pdf with specific path and filename

tjamestx

New Member
Joined
Mar 21, 2014
Messages
9
I have a workbook template that saves to a specific path and filename when the save button is activated. I'd like it to also save it as a .pdf to a different path with the same filename when the save button is activated. I hope that makes sense and I've posted this correctly... The existing code is below. Thank you in advance for any help on this.

Private Sub Save_Button_Click()

If Range("O7").Value = "run" Then
Range("O7").Value = ""

End If

Dim Path As String
Dim FileName As String
Path = "\\server1\share\QuickBooks Common\Templates"

FileName = Range("J13") & " _ " & "PO#" & Range("J12") & "_" & "BOL#" & Range("K7") & "_" & Format(Now(), "yymmdd")

Application.DisplayAlerts = False

ActiveWorkbook.SaveAs FileName:=Path & FileName & ".xlsm", FileFormat:=52

Range("A1").Activate
Range("C9").Activate

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try the following. I've added lines so that you can set a different path for the pdf using the variant pdfpath:
Rich (BB code):
Private Sub Save_Button_Click() If Range("O7").Value = "run" Then Range("O7").Value = "" End If Dim Path As String Dim PdfPath As String Dim FileName As String Path = "\\server1\share\QuickBooks Common\Templates" PdfPath =
"\\server1\share\QuickBooks Common\PDF"
<strike></strike>
FileName = Range("J13") & " _ " & "PO#" & Range("J12") & "_" & "BOL#" & Range("K7") & "_" & Format(Now(), "yymmdd") Application.DisplayAlerts = False ActiveWorkbook.SaveAs FileName:=Path & FileName & ".xlsm", FileFormat:=52
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:=Pdf
<strike></strike>
Path & FileName & ".pdf" Range("A1").Activate Range("C9").Activate End Sub


There are more optional arguments you can use - including setting whether the PDF should be displayed after export - details are on Microsoft's website: https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat
<strike>
</strike>
 
Upvote 0
Solution
Thank you for the response. I copied your modifications (shown below) ran the code and got a "compile error: syntax error."

Private Sub Save_Button_Click()

If Range("O7").Value = "run" Then
Range("O7").Value = ""

End If

Dim Path As String
Dim PdfPath As String
Dim FileName As String
Path = "\\server1\share\QuickBooks Common\Templates"
PdfPath = "\\server1\share\QuickBooks Common\PDF"


FileName = Range("J13") & " _ " & "PO#" & Range("J12") & "_" & "BOL#" & Range("K7") & "_" & Format(Now(), "yymmdd")

Application.DisplayAlerts = False

ActiveWorkbook.SaveAs FileName:=Path & FileName & ".xlsm", FileFormat:=52

ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:=PdfPath & FileName & ".pdf"

Range("A1").Activate
Range("C9").Activate

End Sub
 
Upvote 0
Sorry, on this line I've missed a comma after xlTypePDF:
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=PdfPath & FileName & ".pdf"
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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