Sub OutlookSendMail()
'https://www.mrexcel.com/board/threads/export-an-excel-range-into-outlook.1208614/
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
ActiveWorkbook.SaveCopyAs Filename:=Environ("temp") & "\" & ThisWorkbook.Name
With OutMail
.Display 'This will display the message for you to check and send yourself
'Specify the email subject
.Subject = "add subject here"
'Specify who it should be sent to. Repeat this line to add further recipients. You can use cell references or variables here, too.
.Recipients.Add "Add your 1st Recipient@x.com"
.Recipients.Add "Add your 2nd Recipient@x.com" 'add more lines as necessary
' .Recipients.Type = olBCC
'specify the text to appear in the email
.HTMLBody = RangetoHTML(ThisWorkbook.Sheets(1).Range("A1:O6")) & .HTMLBody
'specify the file to attach
.Attachments.Add Environ("temp") & "\" & ThisWorkbook.Name
' .Paste
'Choose which of the following 2 lines to have commented out
'.Display
'.Send ' This will send the message straight away
End With
Kill Environ("temp") & "\" & ThisWorkbook.Name
End Sub
Function RangetoHTML(Rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
Set TempWB = Workbooks.Add(1)
Rng.Copy
On Error Resume Next
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
'insert rows to put text before the table. This may format weird because you are adding to table.
.Rows("1:4").Insert Shift:=xlDown
.Cells(1, 1) = "Stuff to put in before you paste in the copy range"
.Cells(1, 1).Font.Bold
.Cells(2, 1) = "More lines before the table"
.Cells(3, 1) = "One more line before the table"
.DrawingObjects.Visible = True
' .DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
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
'Read all data from the htm file into RangetoHTML
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=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function