This works for me (Excel & Outlook 2003).
Sub Mail_Selection_Range_Outlook_Body()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2010
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim imageFolder As String, imageFileName As String
Dim HTML As String, p As Long
'Folder containing image to be embedded and the file name of the image - CHANGE AS REQUIRED
imageFolder = "S:\headers\"
imageFileName = "picture.jpg"
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng = Sheets("Order").Range("A15:F26").SpecialCells(xlCellTypeVisible)
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)
'Convert range of cells to HTML string
HTML = RangetoHTML(rng)
'Find HTML body closing tag and insert embedded image reference before it
p = InStr(HTML, "</body>")
HTML = Left(HTML, p - 1) & "<p>The embedded image is shown below.</p><img src='cid:" & imageFileName & "'>" & Mid(HTML, p)
'On Error Resume Next
With OutMail
.To = "email.address@domain.com" 'CHANGE AS REQUIRED
.CC = ""
.BCC = ""
.Subject = "TEST " & Now
.Attachments.Add imageFolder & imageFileName
.HTMLBody = HTML
.Display 'Image embedded in email body
'.Send 'Image appears as an attachment
End With
'On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
The RangeToHTML function is the same as in your post.
As noted in the comment in the code, if you use Send to send the email immediately instead of Display, the image appears as a file attachment rather than an embedded image. If you prefer Send and having the image properly embedded, try the code at http://www.outlookcode.com/d/code/htmlimg.htm, combined with the string manipulation in my code which inserts the image tag in a suitable place in the HTML string.
Bookmarks