Button with macro to save as pdf

Maci3j

Board Regular
Joined
Apr 4, 2013
Messages
83
Office Version
  1. 365
Dear reader,

I have a file: Rapport, with two tabs: 1) Results and 2) Chart.

I would like to create a button on the results-tab, so when I click on it the two tabs (results and chart) wil be saved as pdf-file.

Can you please help me?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,612
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Maci3j,

Try asigning this to your button...

Code:
Sub PDF_Click()
On Error Resume Next
If Not Application.Version > 11.9 Then
Resp = MsgBox("Need to use Excel 2007 or later for this!!", vbOKOnly, "Sorry!")
On Error GoTo 0
Exit Sub
End If
'Set the path to PDF folder  -- edit to suit
MyPath = ThisWorkbook.Path & "\PDFs"  'Same path as the xl file in a subfolder PDFs
MyName = ThisWorkbook.Name
MyName = Left(MyName, InStr(1, MyName, ".") - 1) & ".pdf"

'*** edit out this message if not required
Resp = MsgBox("You will find the PDF file within the folder named 'PDFs'" & Chr(13) & MyPath, _
vbOKOnly, "Creating PDF File")
'******************
Application.ScreenUpdating = False
MkDir MyPath 'folder will be created if not exist otherwise error ignored
Kill MyPath & "\" & MyName 'file will be deleted and overwritten if exist otherwise error ignored
    ThisWorkbook.Sheets(Array("Results", "Chart")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        MyPath & "\" & MyName, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Sheets("Data").Select
On Error GoTo 0
End Sub

Hope that helps.
 

Maci3j

Board Regular
Joined
Apr 4, 2013
Messages
83
Office Version
  1. 365
Maci3j,

Try asigning this to your button...

Code:
Sub PDF_Click()
On Error Resume Next
If Not Application.Version > 11.9 Then
Resp = MsgBox("Need to use Excel 2007 or later for this!!", vbOKOnly, "Sorry!")
On Error GoTo 0
Exit Sub
End If
'Set the path to PDF folder  -- edit to suit
MyPath = ThisWorkbook.Path & "\PDFs"  'Same path as the xl file in a subfolder PDFs
MyName = ThisWorkbook.Name
MyName = Left(MyName, InStr(1, MyName, ".") - 1) & ".pdf"

'*** edit out this message if not required
Resp = MsgBox("You will find the PDF file within the folder named 'PDFs'" & Chr(13) & MyPath, _
vbOKOnly, "Creating PDF File")
'******************
Application.ScreenUpdating = False
MkDir MyPath 'folder will be created if not exist otherwise error ignored
Kill MyPath & "\" & MyName 'file will be deleted and overwritten if exist otherwise error ignored
    ThisWorkbook.Sheets(Array("Results", "Chart")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        MyPath & "\" & MyName, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Sheets("Data").Select
On Error GoTo 0
End Sub

Hope that helps.

Thank you VERY MUCH, i'll try it.
 

Forum statistics

Threads
1,172,166
Messages
5,879,423
Members
433,429
Latest member
Ever

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
Top