VB - Embedd an image into an Outlook email message body
Instead of adding an image to the body of a message with a web
link (img scr='http://somewhere.com'). This code will show how to
embedd the image like when you click on Insert > Picture...
VB Code:
Option Explicit
'Add reference to MS Outlook x.x Object Library
'Picture to be added as an attachment and modified src location for each embedded picture.
Private Sub Command1_Click()
Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment
'create new Outlook MailItem
Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
'add graphic as attachment to Outlook message
'change path to graphic as needed
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("D:\my documents\[color=red]MyPic.jpg[/color]")
oEmail.Close olSave
'change the src property to 'cid:your picture filename'
'it will be changed to the correct cid when its sent.
oEmail.HTMLBody = "<BODY><FONT face=Arial color=#000080 size=2></FONT>" & _
"<IMG alt='' hspace=0 src='[color=red]cid:MyPic.jpg[/color]' align=baseline border=0> </BODY>"
oEmail.Save
oEmail.Display 'fill in the To, Subject, and Send. Or program it in.
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub
When the message is displayed it will look like
its not embedded correctly, but when Outlook sends the
message it will embedd it and link the proper source cid.
Re: VB - Embedd an image into an Outlook email message body
thanks man. a simple Qs. why do i need to save the oEmail?
one thing more, when i removed the attached image and send the images not also showing.
the code work but its kinda odd to attach the image(company logo at complimentary clause) for every mail created?
Re: VB - Embedd an image into an Outlook email message body
How to send picture into new message while Outlook is opening ?
Tell me about do it ?
Thanks !
Re: VB - Embedd an image into an Outlook email message body
Hey i tried this code but i am not getting my image embedded in the body of email. Can u tel me why please. Here is the code:
Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment
'create new Outlook MailItem
Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
'add graphic as attachment to Outlook message
'change path to graphic as needed
Set colAttach = oEmail.Attachments
'Set oAttach = colAttach.Add("D:\my documents\MyPic.jpg")
Set oAttach = colAttach.Add("C:\TITAN\imagefile.jpeg")
'change the src property to 'cid:your picture filename'
'it will be changed to the correct cid when its sent.
With oEmail
.To = "[email protected]"
.Subject = "TITAN DAILY REPORT"
'.Body = "Please find the TITAN daily report pie chart"& vbNewLine& "C:\TITAN\imagefile.jpeg"
'.Body = "Please find the pie chart"
.HTMLBody = "<BODY><FONT face=Arial color=#000080 size=2></FONT>" & _
"<IMG alt='' hspace=0 src='cid:C:\TITAN\imagefile.jpeg' align=baseline border=0> </BODY>"
oEmail.Save
oEmail.Display 'fill in the To, Subject, and Send. Or program it in.
'.Attachments.Add "\\server\drive\folder\filename", olByValue, 1
.Send
End With
'oEmail.Close olSave
MsgBox "Email sent"
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub
Re: VB - Embedd an image into an Outlook email message body
In your related thread in the Classic VB section, see if one of the issues regarding how you are creating the jpg file is the cause.