Send E-Mail for Print Area

united2017

New Member
Joined
Jun 17, 2017
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi All, I have the below code to send the Print Area as a PDF to the E-mail address entered in the worksheet.

It was working all good initially and now states that "Outlook does not recognize one or more names" pointing to the ".send".

Also, is there a way this Macro can name the file "C2" & "-" & "F4"

VBA 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 E2 and cell I7
        
        PDFfile = destFolder & ActiveSheet.Range("f4").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 O3 of active sheet with PDF file attached
        
        Set OutMail = OutApp.CreateItem(olMailItem)
        With OutMail
            .To = ActiveSheet.Range("O3").Value
            .Subject = "Claim"
            .Body = "Please find attached the claim for this month."
            .Attachments.Add PDFfile
            .Send
        End With

        'Delete the temporary PDF file
        
        Kill PDFfile
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    End If
    
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
What is the result of PDFfile?

If you change the code to this, you wouldn't require the Outlook library.

VBA Code:
Sub Button1_Click()
    Dim destFolder As String, PDFfile As String
    Dim printRange As Range
    Dim OutMail As Object

    
    '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 E2 and cell I7
        
        PDFfile = destFolder & ActiveSheet.Range("f4").Value & "-" & ActiveSheet.Range("c2").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 O3 of active sheet with PDF file attached
        
        Set OutMail = CreateObject("Outlook.Application").CreateItem(0)
        With OutMail
            .To = ActiveSheet.Range("O3").Value
            .Subject = "Claim"
            .Body = "Please find attached the claim for this month."
            .Attachments.Add PDFfile
            .display
            '.Send
        End With

        'Delete the temporary PDF file
        
        Kill PDFfile
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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