I'm looking to create a Button on my Quick Access Toolbar (QAT) that will save the current page as a PDF to the same location as the open excel file. I can get a Macro set to the button I add to the QAT, but trying to get the Macro to work throughout any excel spreadsheet I open is not coming to me. I found the following code to get it to save all the pages in the current location with the tab name as the file name:
And this code to save the active sheet in a certain location:
I'd like to save only the active sheet (2nd code), but use the "Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf" from the first code to always save the PDF to the current folder. I've tried cutting and pasting, but keep getting errors.
Thanks for any help.
I know there is a pre-set option to "Publish as PDF or XPS", but that still requires 2 or 3 clicks, not just one, like word allows me to do.
VBA Code:
Sub LoopSheetsSaveAsPDF()
'Create variables
Dim ws As Worksheet
'Loop through all worksheets and save as individual PDF in same folder
'as the Excel file
For Each ws In ActiveWorkbook.Worksheets
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf"
Next
End Sub
And this code to save the active sheet in a certain location:
VBA Code:
Sub SavePDF()
'Create and assign variables
Dim saveLocation As String
saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"
'Save Active Sheet(s) as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation
End Sub
I'd like to save only the active sheet (2nd code), but use the "Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf" from the first code to always save the PDF to the current folder. I've tried cutting and pasting, but keep getting errors.
Thanks for any help.
I know there is a pre-set option to "Publish as PDF or XPS", but that still requires 2 or 3 clicks, not just one, like word allows me to do.