Embedding Image in Email with Vba

Embed picture in outlook mail body excel vba

You need to add the image and hide it. The position 0 will add and hide it.

.Attachments.Add Fname, 1, 0

The 1 is the Outlook Constant olByValue

Once you add the image then you have to use "cid:FILENAME.jpg" as shown below.

Try this

With OutMail
.To = tName
.CC = ""
.BCC = ""
.Subject = STAT.Range("C1").Value
.Attachments.Add Fname, 1, 0
.HTMLBody = "<html><p>Summary of Claim Status.</p>" & _
"<img src=""cid:Claims.jpg""height=520 width=750>"
.Display
End With

Screenshot

Sample Image

How to embed an image into an Outlook email using VBA

The cid is created when you attach it, so you need to do that before you display/send it.

Try it like this

Set OutMail = outApp.CreateItem(0)
With OutMail
.To = WB.Names("to").RefersToRange.Value2
.CC = WB.Names("cc").RefersToRange.Value2
.BCC = WB.Names("bcc").RefersToRange.Value2
.Subject = WB.Names("Subject").RefersToRange.Value2
If Attchmnt <> "" Then
.Attachments.Add Attchmnt ' (additional arguments are optional)
.HTMLBody = "<img src=""cid:Painted_Lady_Migration.jpg"" height=520 width=750>"
Else
.HTMLBody = "[no attachment included]"
End If
.Display
End With

Embedded image not showing on email VBA

The issue lied with the HTML Body statement. I added quotes and it's now embedding correctly.

Sub Email()

'Sends the last saved version of the Activeworkbook
Dim OutApp As Object
Dim OutMail As Object
Dim Filepath As String
Dim Filename As String

Filename = Sheets("Private").Range("C19").Value
Filepath = Environ("USERPROFILE") & "\Downloads\" & Filename

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

On Error Resume Next
With OutMail
.To = Worksheets("Private").Range("A19").Value
'.BCC =
.Subject = Worksheets("Private").Range("H29").Value
'.Body =
.Attachments.Add ActiveWorkbook.FullName
.Attachments.Add Filepath, olByValue, 0
'Change "1" value to 0 to hide
.HTMLBody = "<img src=""" & Filepath & """>"
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")

.Display 'or use .Send

End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Embed an image in the body of an email

Use Word's InlineShapes.AddPicture Method

The procedure is described in Working with Item Bodies.

Inserting pictures:

strFile = "C:\Pictures\logo.gif"
Set objInsp = objMsg.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
If objMsg.BodyFormat <> olFormatPlain Then
objSel.InlineShapes.AddPicture strFile, False, True
End If


Related Topics



Leave a reply



Submit