VBA - Excel Worksheet Selected Range to PDF - Email via Outlook

united2017

New Member
Joined
Jun 17, 2017
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Dear Forumers,

I am very new to the concept of VBA. I have a workbook with an identical template in each sheet. Each sheet represents different areas of the business. What I want to do is, when I have all the information in these sheets, I want to send the selection of the print area as a PDF to an e-mail address in a given cell in the worksheet. Each worksheet has a unique e-mail address.
So once I finish the data on 'Sheet 1', I want to run the macro to send e-mail to the address in for example A1 of that sheet, than the same for sheet 2, 3, etc.

It has to be the selected print area, as there will be other workings on the same sheet which I do not want to send out.

Your help will be highly appreciated.

Thanks,
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this code in a standard module. You must set a reference to the Outlook nn.0 Object Library, via Tools -> References in the VBA editor.

Code:
Public Sub Send_Emails_For_Print_Areas()

    Dim destFolder As String, PDFfile As String
    Dim wsh As Worksheet
    Dim printRange As Range
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem

    Set OutApp = New Outlook.Application
    
    'PDF file for each print range is temporarily saved in same folder as this workbook
    
    destFolder = ThisWorkbook.Path & "\"
    If Right(destFolder, 1) <> "\" Then destFolder = destFolder & "\"
    
    For Each wsh In ActiveWorkbook.Worksheets
    
        If wsh.PageSetup.PrintArea <> "" Then
        
            'Save print area for this sheet as a PDF file
            
            PDFfile = destFolder & wsh.Name & ".pdf"
            Set printRange = Range(wsh.PageSetup.PrintArea)
            printRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfile, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        
            'Send email to address in cell A1 of this sheet with PDF file attached
            
            Set OutMail = OutApp.CreateItem(olMailItem)
            With OutMail
                .To = wsh.Range("A1").Value
                .Subject = "Subject line"
                .Body = "Body text"
                .Attachments.Add PDFfile
                .send
            End With

            'Delete the temporary PDF file
            
            Kill PDFfile
        
        End If

    Next
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    
End Sub
 
Upvote 0
Thanks John, the above code works well, just a few tweaks required please:
- When I run the macro, all the worksheets are e-mailed out, is it possible to only e-mail the active worksheet, as the rest have not been completed?
- The file name goes as the worksheet name, i.e. 'Sheet1', is it possible to have that changed to a standard cell on each worksheet?

Many Thanks
 
Upvote 0
With the two tweaks:
Code:
Public Sub Send_Email_For_Print_Area()

    Dim destFolder As String, PDFfile As String
    Dim printRange As Range
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem

    Set OutApp = New Outlook.Application
    
    'PDF file for print range is temporarily saved in same folder as this workbook
    
    destFolder = ThisWorkbook.Path & "\"
    If Right(destFolder, 1) <> "\" Then destFolder = destFolder & "\"
    
    If ActiveSheet.PageSetup.PrintArea <> "" Then
    
        'Save print area for active sheet as a PDF file, file name from cell A2
        
        PDFfile = destFolder & ActiveSheet.Range("A2").Value & ".pdf"
        Set printRange = Range(ActiveSheet.PageSetup.PrintArea)
        printRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfile, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    
        'Send email to address in cell A1 of active sheet with PDF file attached
        
        Set OutMail = OutApp.CreateItem(olMailItem)
        With OutMail
            .To = ActiveSheet.Range("A1").Value
            .Subject = "Subject line"
            .Body = "Body text"
            .Attachments.Add PDFfile
            .send
        End With

        'Delete the temporary PDF file
        
        Kill PDFfile
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    End If
    
End Sub
 
Upvote 0
John--Thanks for posting this...I'm trying to do something similar with VB code. I want to save a worksheet print area to a pdf and then email that opens outlook. I further tweaked the above code but when I run the above code I get an error message. I don't have the email in cell A1 nor the document name is cell A2. Can you help? Thanks in advance and much appreciated.
 
Upvote 0
I further tweaked the above code but when I run the above code I get an error message. I don't have the email in cell A1 nor the document name is cell A2. Can you help? Thanks in advance and much appreciated.
Post your code. What is the exact error message and which line causes it?

Here is the code changed to hard-code the email address and file name, instead of getting them from A1 and A2:
Code:
Public Sub Send_Email_For_Print_Area()

    Dim destFolder As String, PDFfile As String
    Dim printRange As Range
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem

    Set OutApp = New Outlook.Application
    
    'PDF file for print range is temporarily saved in same folder as this workbook
    
    destFolder = ThisWorkbook.Path & "\"
    If Right(destFolder, 1) <> "\" Then destFolder = destFolder & "\"
    
    If ActiveSheet.PageSetup.PrintArea <> "" Then
    
        'Save print area for active sheet as a PDF file
        
        PDFfile = destFolder & "My Data.pdf"
        Set printRange = Range(ActiveSheet.PageSetup.PrintArea)
        printRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfile, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    
        'Send email with PDF file attached
        
        Set OutMail = OutApp.CreateItem(olMailItem)
        With OutMail
            .To = "email.address@email.com" ' CHANGE THIS
            .Subject = "Subject line"
            .Body = "Body text"
            .Attachments.Add PDFfile
            .send
        End With

        'Delete the temporary PDF file
        
        Kill PDFfile
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    End If
    
End Sub
 
Last edited:
Upvote 0
The error I get now is Kill PDFfile

Here is the code I inserted:

Sub Send_PDF_Via_Email()
Dim destFolder As String, PDFfile As String
Dim printRange As Range
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = New Outlook.Application

'PDF file for print range is temporarily saved in same folder as this workbook

destFolder = ThisWorkbook.Path & ""
If Right(destFolder, 1) <> "" Then destFolder = destFolder & ""

If ActiveSheet.PageSetup.PrintArea <> "" Then

'Save print area for active sheet as a PDF file

PDFfile = destFolder & "My Data.pdf"
Set printRange = Range(ActiveSheet.PageSetup.PrintArea)
printRange.ExportAsFixedFormat Type:=xlTypePDF, FileName:=PDFfile, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

'Send email with PDF file attached

Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = ""
.Subject = ""
.Body = ""
.Attachments.Add PDFfile

End With
'Delete the temporary PDF file

Kill PDFfile

Set OutMail = Nothing
Set OutApp = Nothing

End If

End Sub
 
Upvote 0
Ensure you have the trailing back slash on the folder path:
Code:
    destFolder = ThisWorkbook.Path & "\"
    If Right(destFolder, 1) <> "\" Then destFolder = destFolder & "\"
Please use CODE tags.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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