VBA Code - Excel save as .pdf and email

Matrix007

New Member
Joined
Nov 8, 2017
Messages
14
Hi

I am after a VBA code to select certain sheets of my excel workbook .xlsx, convert them to .pdf and email it via outlook.

Thanking in advance.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Below should start to help you out.

This saves the PDFs in a folder in the same location as the workbook itself (you will need to create this folder) & then creates emails and attaches them. You'll need to add some things in and you can amend the details of the email itself if you wish.



Sub SavetoPDFandEmail()

Dim ws As Worksheet
Dim PdfFile As String
Dim OutlookObj As Object
Dim EmailObj As Object
Dim PdfPath

With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Application.DisplayAlerts = False


'ws not to be included
For Each ws In ActiveWorkbook.Worksheets
Select Case UCase(ws.Name)
Case **worksheet names not to be included**
Case Else
ws.Activate

'Define PDF filename
PdfFile = **pdf file name required**

'Check file location
Set thisWb = ActiveWorkbook


'Export activesheet as PDF
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=thisWb.Path & **insert folder name in same location as the file saved** & PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With

'Create Outlook email
Set OutlookObj = CreateObject("Outlook.Application")
Set EmailObj = OutlookObj.CreateItem(0)
With EmailObj
.Display
.To = ""
.CC = ""
.Subject = ""
.Body = ""
.Attachments.Add Filename
End With
End Select
Next
End Sub
 
Upvote 0
Hi Thank you.

The excel file is an email file which users will fill and need to send as a pdf.
There is no dedicated location. Options please..?
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
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