Excel Button to Save as PDF

MattQAQC

New Member
Joined
Jul 21, 2022
Messages
3
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi,

i have a button on my spreadsheet to save the sheet as a PDF but i can figure out how to get it to create a copy of the file in excel with the same name and save both that copy and the PDF in the location of where the file original file is saved

this is my code currently:

Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Range("P2"), _
OpenAfterPublish:=True
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi Matt

This what i use at the moment

Private Sub CommandButton1_Click()

FN = Range("Save to") & Range("Save as")
FN_PDF = FN & ".pdf"
FN_XLSM = FN & ".xlsm"


'Save as a PDF
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FN_PDF, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True

Application.DisplayAlerts = False 'Set False, so not warned about over writing the files.

'Save as a macro enabled Excel file (e.g. *.xlsm)
ActiveWorkbook.SaveAs Filename:=FN_XLSM, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Application.DisplayAlerts = True 'Set back to true if needed...


End Sub

Works well for me but interested to see what others come up with.
My version doesnt like saving with the same name as already open.
 
Upvote 0
Hi Matt

This what i use at the moment

Private Sub CommandButton1_Click()

FN = Range("Save to") & Range("Save as")
FN_PDF = FN & ".pdf"
FN_XLSM = FN & ".xlsm"


'Save as a PDF
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FN_PDF, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True

Application.DisplayAlerts = False 'Set False, so not warned about over writing the files.

'Save as a macro enabled Excel file (e.g. *.xlsm)
ActiveWorkbook.SaveAs Filename:=FN_XLSM, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Application.DisplayAlerts = True 'Set back to true if needed...


End Sub

Works well for me but interested to see what others come up with.
My version doesnt like saving with the same name as already open.
Thanks mate.

where is the file location on your code or does it not need it?

i have it set currently to save as a file name in a certain cell. do you know what i would chnage in yours to keep that or what to just add to mine to save in same file location?
 
Upvote 0
1-Quotation Blank NEW IES.xlsm
ABCDEFGHIJKLMNTUV
1
2QUOTATIONCustomer:
3Job Ref:
4Quote No: 85476Project Length:
5Date:21/07/2022
6By:NET TOTAL =£0.00
7VAT =£0.00
8TOTAL =£0.00
9ItemSizeUnit NetQtyNet Total
10£0.00
11£0.00
12£0.00
13£0.00
14£0.00
15£0.00
16£0.00
17£0.00
18£0.00
19£0.00
20£0.00
21£0.00
22£0.00
23£0.00
24£0.00
25£0.00
26£0.00
27£0.00
28£0.00
29£0.00
30£0.00
31£0.00
32£0.00
33£0.00
34£0.00
35£0.00
36£0.00
37£0.00
38£0.00
39£0.00
40£0.00
41£0.00
42£0.00
43£0.00
44£0.00
45£0.00
46£0.00
47£0.00
48£0.00
49£0.00
50
51
52
53
54
55
56
57
Quote
Cell Formulas
RangeFormula
E4E4=LEFT(I3,6)
F4F4=S5+D5
D5D5=NOW()
K6K6=SUM((K10:K15)+(K17:K22)+(K24:K29))
K7K7=K6*0.2
K8K8=K6+K7
K10:K49K10=SUM(I10*J10)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
K10:K49Cell Valuecontains "0"textNO
 
Upvote 0
Hi Matt, in the row FN = Range("Save to") & Range("Save as"), enter the cell with you save location in place of "Save to" and the cell with your document title inplace of "Save to"
So if the address you wanted to save the document to was in cell A1 and the title you wanted to save it as was in cell A2 it would be FN = Range(A1) & Range(A2)
Don't forget the \ at the end of the address.
Hope this helps
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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