pdf save one touch button

realred2

New Member
Joined
Jun 22, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
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:

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.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Excel Formula:
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
 
Upvote 0
Excel Formula:
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

This is the first set of code and still saves all the worksheets as individual PDF's, I only want to save the currently active worksheet.
Thanks
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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