Send Email with Excel data included in email

kiwikiki718

Board Regular
Joined
Apr 7, 2017
Messages
80
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet that I want to send the data only of specific cells in a email. The Cells that I want to display in the actual body of the email is A1:A8 & B1:B8 which is on sheet 2. please help here is the code that I have that only attaches the spreadsheet to the email.


Sub Mail_workbook_Outlook()

Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next

With OutMail
.To = ""
.CC = ""
.Subject = "Test Email"
.body = "I will like to Request access to the following. "
.Attachments.Add ActiveWorkbook.FullName
.send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing


MsgBox "Your request has been sent "
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Here's an article to do exactly what you're requesting: https://www.rondebruin.nl/win/s1/outlook/bmail2.htm

I highly recommend reading through some of the posts here too: https://www.rondebruin.nl/win/s1/outlook/mail.htm

In this example, update rng to reflect the range you want to add to your email body. For testing, I recommend using .Display to show you the email without sending. Once your code is complete, you can flip it back to .Send
Code:
<!--StartFragment-->Sub Mail_Selection_Range_Outlook_Body()
[COLOR=black]'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016[/COLOR]
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    [COLOR=black]'Only the visible cells in the selection[/COLOR]
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    [COLOR=black]'You can also use a fixed range if you want
    [/COLOR][COLOR=#ff0000]Set rng = Sheets("Sheet2").Range("A1:B8").SpecialCells(xlCellTypeVisible)[/COLOR][COLOR=black][/COLOR]
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

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

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

    On Error Resume Next
    With OutMail
        .To = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Display   [COLOR=black]'or use .Send[/COLOR]
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
[COLOR=black]' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object[/COLOR]
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    [COLOR=black]'Copy the range and create a new workbook to past the data in[/COLOR]
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    [COLOR=black]'Publish the sheet to a htm file[/COLOR]
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    [COLOR=black]'Read all data from the htm file into RangetoHTML[/COLOR]
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    [COLOR=black]'Close TempWB[/COLOR]
    TempWB.Close savechanges:=False

    [COLOR=black]'Delete the htm file we used in this function[/COLOR]
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function<!--EndFragment-->
 
Upvote 0

Forum statistics

Threads
1,223,764
Messages
6,174,364
Members
452,558
Latest member
jswan83

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