VBA how can I email a range as PDF

Siimo

New Member
Joined
Jan 9, 2017
Messages
12
Hi,

I am wondering if anyone can help.

I am looking to use VBA to email a range on the active sheet the button is on. Range is B7:M33.

I would like the range to be attached to an email in PDF format, I have a another sheet called "Setup" which has the email addresses, subject and body. I am confident coding the to/cc/subject/body etc, however I am having issues creating the PDF.

Ideally I don't want the coding to save the PDF, or if it does have to be saved, can it be saved to desktop and then deleted after the email is sent automatically?

Thank you in advance for any help.

ps. I am using Office 360 so the latest version of excel 2016 if this makes a difference.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try this, changing the name of the PDF file where indicated. The email settings are in the "Setup" sheet cells B1:B4.
Code:
Public Sub Email_Range_In_PDF()

    Dim saveRange As Range
    Dim setupSheet As Worksheet
    Dim PDFfile As String
    Dim OutApp As Object
    Dim OutMail As Object
    
    Set saveRange = ActiveSheet.Range("B7:M33")
    Set setupSheet = Worksheets("Setup")
    
    PDFfile = Environ("temp") & "\Range B7 To M33.pdf"      'CHANGE FILE NAME

    saveRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfile, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = setupSheet.Range("B1").Value
        .CC = setupSheet.Range("B2").Value
        .Subject = setupSheet.Range("B3").Value
        .Body = setupSheet.Range("B4").Value
        .Attachments.Add PDFfile
        .Send
    End With
    On Error GoTo 0
    
    Kill PDFfile
    
    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub
 
Upvote 0
John_w thank you this works great just what I needed.

Very much appreciate your time with this, was taking me hours researching different codes online, this one is simple and exactly how I needed it to work.

Regards
 
Upvote 0

Forum statistics

Threads
1,223,996
Messages
6,175,861
Members
452,676
Latest member
woodyp

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