This code in Excel (Office 365) would add an Excel range or table to a mail body.
The crucial part is for it to work in the background w/o the mail client (Outlook) being open.
I have working code (with Outlook in the background). It can only link specific or merged cells to mail body (which essentially means text and not even a simple table).
When linking a proper range (with let's say three columns and five rows), I get
"Run-time error '13': Type mismatch"
Sub Email()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "E-MAIL BODY TEXT" & vbNewLine & _
Range("ARRAY")
On Error Resume Next
With xOutMail
.To = "test@mail.com"
.CC = ""
.BCC = ""
.Subject = "Mail subject"
.Body = xMailBody
.Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
I have other code which is able to grab a range (sort of) from the sheet and send it as a table. It only runs when Outlook is open and running & producing an error message otherwise.
Sub Email2()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
Dim lr As Integer
lr = sh.Range("D" & Application.Rows.Count).End(xlUp).Row
sh.Range("D5:F" & lr).Select
With Selection.Parent.MailEnvelope.Item
.to = "test@mail.com"
.bcc = ""
.Subject = ""
.send
End With
End Sub