Button with macro to save as pdf

Maci3j

Board Regular
Joined
Apr 4, 2013
Messages
79
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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,287
Office Version
  1. 2013
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
79
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,136,353
Messages
5,675,283
Members
419,559
Latest member
BraytonM

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