Saving an Excel as a PDF using VBA

p4nny

Board Regular
Joined
Jan 13, 2015
Messages
246
Hi,

I would like to save a tab called "Booking form" as a PDF

AND

for the filename to be based on 2 textboxes within a userform within the same workbook.

Hope that makes sense.

Appreciate the help, as always!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This code saves in pdf sheet " Sheet2 "

Code:
Sub SaveSheetsToPdf() Dim filename As String
 filename = Range("A2") & Range("B2") & ".pdf"
 Sheets("Sheet2").ExportAsFixedFormat Type:=xlTypePDF, _
 filename:=ActiveWorkbook.Path & filename & ".pdf", _
 Quality:=xlQualityStandard, _
 IncludeDocProperties:=True, _
 IgnorePrintAreas:=False, _
 OpenAfterPublish:=True 'False not visualizzation
End Sub
 
Upvote 0
Or like this?

Code:
Private Sub CommandButton1_Click()
    Application.DisplayAlerts = False
        ActiveWorkbook.Sheets("Booking form").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            ActiveWorkbook.Path & "\" & TextBox1 & " " & TextBox2 & ".pdf" _
                , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=True    '<---- or False
    Application.DisplayAlerts = True
    Unload Me
End Sub

@makexel
I think you forgot the backslash for saving
 
Last edited:
Upvote 0
Or like this?

Code:
Private Sub CommandButton1_Click()
    Application.DisplayAlerts = False
        ActiveWorkbook.Sheets("Booking form").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            ActiveWorkbook.Path & "\" & TextBox1 & " " & TextBox2 & ".pdf" _
                , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=True    '<---- or False
    Application.DisplayAlerts = True
    Unload Me
End Sub

@makexel
I think you forgot the backslash for saving

ok , I will pay more attention , I had tested , thanks ;)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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