PDA

View Full Version : [SOLVED:] Embed image into body mail



slamet Harto
02-25-2010, 02:59 AM
Hi there.

Hope is everything running well.

Please help me on how to embed image into bodymail using excel and outlook.

Here is what i've done

sub test()
Dim OL As Object, rng As Range
Dim EmailItem As Object
Const mypath As String = "d:\My Pictures\"
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(0)
EmailItem.Attachments.Add mypath & "AYANA-emailblast-100211.jpg"
EmailItem.HTMLBody = "<html><p>This is a picture.</p>" & _
"<img src='cid:pictest.jpg' height=480 width=360>"
EmailItem.Display
End sub

I can't see the picture with the above code
Please find the attached sample picture.

Thank you in advance.
Regards,
Harto

lucas
02-25-2010, 09:36 AM
Try this Harto:


Sub test()
Dim OL As Object, rng As Range
Dim EmailItem As Object
Const mypath As String = "d:\My Pictures\"
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(0)
' EmailItem.Attachments.Add mypath & "AYANA-emailblast-100211.jpg"
EmailItem.HTMLBody = "<html><p>This is a picture.</p>" & _
"<img src=C:\Users\Steve\Documents\pictest.jpg' height=480 width=360>"
EmailItem.Display
End Sub

slamet Harto
02-26-2010, 07:26 AM
Thanks for the reply, Lucas.

i'll test the code given later as we've public holiday here.
Have a nice weekend

slamet Harto
02-28-2010, 09:09 PM
Hi Steve,

Still have the same problem. I can;t see the picture.

Please advise
Thanks a bunch

lucas
03-01-2010, 08:10 AM
Harto, it works consistantly for me. You should double check your path and filename.

It displays rather than sends and the picture is there for me.

slamet Harto
03-01-2010, 08:57 PM
hi Steve.
Ok then. perhaps it need a bit change on My PC setting.

Have a great day!

mac123
08-19-2013, 08:53 AM
Worked perfectrly!

:cloud9:


Try this Harto:

Sub test()
Dim OL As Object, rng As Range
Dim EmailItem As Object
Const mypath As String = "d:\My Pictures\"
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(0)
' EmailItem.Attachments.Add mypath & "AYANA-emailblast-100211.jpg"
EmailItem.HTMLBody = "<html><p>This is a picture.</p>" & _
"<img src=C:\Users\Steve\Documents\pictest.jpg' height=480 width=360>"
EmailItem.Display
End Sub

ZVI
08-19-2013, 08:39 PM
Worked perfectrly!
I afraid, the image is linked to your local file instead of embedded.
In this case picture will not be displayed on recipient side (if it's not yours).
Try this:


Sub EmbedPicture()
Const MyPath = "C:\Temp\"
Const MyPicture = "My Picture.jpg"
With CreateObject("Outlook.Application").CreateItem(0)
.Attachments.Add MyPath & MyPicture
.HTMLBody = "<html><p>This is a picture</p>" & _
"<img src=cid:" & Replace(MyPicture, " ", "%20") & " height=240 width=180>" & _
"<p>Best Regards,</p>" & _
"<p>" & UCase(Environ("USERNAME")) & "</p></html>"
.Display
End With
End Sub

KIRAN.SHETTY
03-25-2019, 08:07 AM
I afraid, the image is linked to your local file instead of embedded.
In this case picture will not be displayed on recipient side (if it's not yours).
Try this:


Sub EmbedPicture()
Const MyPath = "C:\Temp\"
Const MyPicture = "My Picture.jpg"
With CreateObject("Outlook.Application").CreateItem(0)
.Attachments.Add MyPath & MyPicture
.HTMLBody = "<html><p>This is a picture</p>" & _
"<img src=cid:" & Replace(MyPicture, " ", "%20") & " height=240 width=180>" & _
"<p>Best Regards,</p>" & _
"<p>" & UCase(Environ("USERNAME")) & "</p></html>"
.Display
End With
End Sub



"How can you make this dyncamic to ensure that it picks different images based on situation supplied"
"Const MyPicture = "My Picture.jpg""

ZVI
03-25-2019, 05:33 PM
"How can you make this dyncamic to ensure that it picks different images based on situation supplied"
"Const MyPicture = "My Picture.jpg""
Hi and welcome to VbaExpress forum!
This code displays file picker dialog to choose an image and embed it into email:

Sub Main() Dim PathName As String
With Application.FileDialog(msoFileDialogFilePicker)
'.InitialFileName = "D:\Temp\" ' <-- Use this line to set a default folder
If .Filters.Count > 0 Then .Filters.Delete
.Filters.Add "Images", "*.gif; *.png; *.jpg; *.jpeg", 1
.AllowMultiSelect = False
If .Show = False Then Exit Sub
PathName = .SelectedItems(1)
End With
Call EmbedPicture(PathName)
End Sub


Sub EmbedPicture(PathName As String)
Dim MyPicture As String
MyPicture = Mid(PathName, InStrRev(PathName, Application.PathSeparator) + 1)
With CreateObject("Outlook.Application").CreateItem(0)
.Attachments.Add PathName
.HTMLBody = "<html><p>This is a picture</p>" & _
"<img src=cid:" & Replace(MyPicture, " ", "%20") & _
"<p>Best Regards,</p>" & _
"<p>" & UCase(Environ("USERNAME")) & "</p></html>"
.Display
End With
End Sub
Regards

KIRAN.SHETTY
03-26-2019, 03:07 AM
Hi and welcome to VbaExpress forum!
This code displays file picker dialog to choose an image and embed it into email:

Sub Main() Dim PathName As String
With Application.FileDialog(msoFileDialogFilePicker)
'.InitialFileName = "D:\Temp\" ' <-- Use this line to set a default folder
If .Filters.Count > 0 Then .Filters.Delete
.Filters.Add "Images", "*.gif; *.png; *.jpg; *.jpeg", 1
.AllowMultiSelect = False
If .Show = False Then Exit Sub
PathName = .SelectedItems(1)
End With
Call EmbedPicture(PathName)
End Sub


Sub EmbedPicture(PathName As String)
Dim MyPicture As String
MyPicture = Mid(PathName, InStrRev(PathName, Application.PathSeparator) + 1)
With CreateObject("Outlook.Application").CreateItem(0)
.Attachments.Add PathName
.HTMLBody = "<html><p>This is a picture</p>" & _
"<img src=cid:" & Replace(MyPicture, " ", "%20") & _
"<p>Best Regards,</p>" & _
"<p>" & UCase(Environ("USERNAME")) & "</p></html>"
.Display
End With
End Sub
Regards

"Thanks, It worked just as expected, Thanks again.

KIRAN.SHETTY
03-28-2019, 03:26 AM
Hi and welcome to VbaExpress forum!
This code displays file picker dialog to choose an image and embed it into email:

Sub Main() Dim PathName As String
With Application.FileDialog(msoFileDialogFilePicker)
'.InitialFileName = "D:\Temp\" ' <-- Use this line to set a default folder
If .Filters.Count > 0 Then .Filters.Delete
.Filters.Add "Images", "*.gif; *.png; *.jpg; *.jpeg", 1
.AllowMultiSelect = False
If .Show = False Then Exit Sub
PathName = .SelectedItems(1)
End With
Call EmbedPicture(PathName)
End Sub


Sub EmbedPicture(PathName As String)
Dim MyPicture As String
MyPicture = Mid(PathName, InStrRev(PathName, Application.PathSeparator) + 1)
With CreateObject("Outlook.Application").CreateItem(0)
.Attachments.Add PathName
.HTMLBody = "<html><p>This is a picture</p>" & _
"<img src=cid:" & Replace(MyPicture, " ", "%20") & _
"<p>Best Regards,</p>" & _
"<p>" & UCase(Environ("USERNAME")) & "</p></html>"
.Display
End With
End Sub
Regards

"Sorry to be coming abck time and again, The code did work however when the email was being sent to external email id , the images never displayed on the body of the email and comes with an X mark.

Is there a way to get this sorted"