Inserting a JPG file into the body of an email

NewOrderFac33

Well-known Member
Joined
Sep 26, 2011
Messages
1,275
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Good afternoon,
I'm generating an email in VBA and I want to include a JPEG file in the body of the email:

I currently have:
Code:
Set InfoSheet = Sheets("InfoSheet")
Dim PicFile As Range
Set PicFile = InfoSheet.Range("PicFile")
MsgBody = MsgBody & "<HTML><BODY><IMG src='PicFile' /></BODY></HTML>"
where "PicFile" is a worksheet cell contaniing the full pathname of a JPEG file.
Hpwever, when I run the following code:
Code:
With OutMail
        .To = Email_To
        .CC = Email_CC
        .Subject = Email_Subject
        .HTMLbody = MsgBody
        '.attachments.Add Email_Attachment_10
        .Display
        '.Send
    End With
my previewed message includes the pathname of the .jpg file as a string, but not the .jpg itself.
Can anyone give me a pointer as to where I'm going wrong, please?
Thanks in advance
Pete
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Code:
With objMail
        .To = "XXXXYYYY@gmail.com"
        .CC = ""
        .BCC = ""
        .Subject = "Your picture"
        .Attachments.Add myPath & FileName1, 1, 0
        .HTMLBody = "<html>" & _
                "<p style=""text-align:center"">Hi,<br>As you request</p>" & _
                "<p style=""text-align:center""><img src=""cid:" & FileName1 & """ height=520 width=750></p>" & _
                "<p style=""text-align:center"">Some more text here.. </p>"
                "</html>"
        .display
    End With
FileName1 attached and in HTMLBody put as IMG src
 
Upvote 0
Good afternoon - that's a massive improvement on what I had before - it's just that it's now saying "The linked image can not be displayed", so I guess that's one for ME to solve now - just checking for hidden spaces in the filename - all that sort of thing!
Thanks
Pete
 
Upvote 0
OK, so now I'm using the following:
Code:
Set InfoSheet = Sheets("InfoSheet")
Set PicFile_David = InfoSheet.Range("PicFile_David")
MsgBody = MsgBody & "<p style=""text-align:left""><img src=""cid:" & PicFile_David & """ height=250 width=1000></p>"

With OutMail
        .To = Email_To
        .CC = Email_CC
        .Subject = Email_Subject
        .HTMLBody = MsgBody
        .display
        '.Send
    End With
The placeholder is displayed with "The linked image can not be displayed" and a red cross.
I can insert the file that is referenced in Set PicFile_David = InfoSheet.Range("PicFile_David")
manually and it is displayed correctly, so I know that the filename is correct.

Can anyone think of anything else that I might be doing incorrectly?

Thanks in advance

Pete
 
Upvote 0
Pete

Where is the image file you want to add located?
 
Upvote 0
Pete

Where is the image file you want to add located?
It's on a network drive. If I Insert Picture from the menu, it is inserted into the email correctly.
I have tried specifying the file location using the full path i.e. \\OURSERVER\MyFolder\MyFile and using the drive letter i.e. K:\MyFolder\MyFile, but neither work.
 
Upvote 0
OK, so now I'm using the following:
Code:
Set InfoSheet = Sheets("InfoSheet")
Set PicFile_David = InfoSheet.Range("PicFile_David")
MsgBody = MsgBody & "<ptext-align:left""><img src=""cid:" & PicFile_David & """ height=250 width=1000></p>"

With OutMail
        .To = Email_To
        .CC = Email_CC
        .Subject = Email_Subject
        .HTMLBody = MsgBody
        .display
        '.Send
    End With

there is missing:

Code:
.Attachments.Add PicFile_David, 1, 0
in With OutMail

and imho do like below:

Code:
PicFile_David = InfoSheet.Range("PicFile_David").Value
if PicFile_David is range for cell (ex. A1) where proper full path + file name is set, like K:\MyFolder\MyFile.jpg

Try this code

Code:
Sub CopyImagesToMail()
    Dim objWorksheet As Excel.Worksheet
    Dim objOutlookApp, objMail, objMailDocument As Object
    Dim objShape As Excel.Shape
    Dim FileName1, FileName2, myPath As String

    myPath = "Z:\"                                     ' set as you path, of course can be ref to cell like Sheets("MySheet").Range("B1).Value as it is just string
    FileName1 = "1.jpg"                            ' set filename as you want to, of course it can be Sheets("MySheet").Range("A1").Value as filename is just string

    Set objWorksheet = ThisWorkbook.Worksheets(1)
    Set objOutlookApp = CreateObject("Outlook.application")
    Set objMail = objOutlookApp.CreateItem(objOutlookAppobjMailItem)

     With objMail
        .To = "XXXXYYYY@gmail.com"
        .CC = ""
        .BCC = ""
        .Subject = "Your pictures"
        .Attachments.Add myPath & FileName1, 1, 0
        .HTMLBody = "<html>" & _
                "<p style=""text-align:center"">Hi,<br>As you request</p>" & _
                "<p style=""text-align:center""><img src=""cid:" & FileName1 & """ height=520 width=750></p>" & _
                "<p style=""text-align:center"">Some more text here.. </p>"
                "</html>"
        .display    'or .Send if you want to send in background without editing emal
    End With
End Sub
if works perfectly for me.
 
Last edited:
Upvote 0
there is missing:

Code:
.Attachments.Add PicFile_David, 1, 0
in With OutMail

and imho do like below:

Code:
PicFile_David = InfoSheet.Range("PicFile_David").Value
if PicFile_David is range for cell (ex. A1) where proper full path + file name is set, like K:\MyFolder\MyFile.jpg

Try this code

Code:
Sub CopyImagesToMail()
    Dim objWorksheet As Excel.Worksheet
    Dim objOutlookApp, objMail, objMailDocument As Object
    Dim objShape As Excel.Shape
    Dim FileName1, FileName2, myPath As String

    myPath = "Z:\"                                     ' set as you path, of course can be ref to cell like Sheets("MySheet").Range("B1).Value as it is just string
    FileName1 = "1.jpg"                            ' set filename as you want to, of course it can be Sheets("MySheet").Range("A1").Value as filename is just string

    Set objWorksheet = ThisWorkbook.Worksheets(1)
    Set objOutlookApp = CreateObject("Outlook.application")
    Set objMail = objOutlookApp.CreateItem(objOutlookAppobjMailItem)

     With objMail
        .To = "XXXXYYYY@gmail.com"
        .CC = ""
        .BCC = ""
        .Subject = "Your pictures"
        .Attachments.Add myPath & FileName1, 1, 0
        .HTMLBody = "<html>" & _
                "<p style=""text-align:center"">Hi,<br>As you request</p>" & _
                "<p style=""text-align:center""><img src=""cid:" & FileName1 & """ height=520 width=750></p>" & _
                "<p style=""text-align:center"">Some more text here.. </p>"
                "</html>"
        .display    'or .Send if you want to send in background without editing emal
    End With
End Sub
if works perfectly for me.

Good afternoon, Kokosek!
I have no issues with adding JPGs as attachments - it's including them in the body of the email that I'm having trouble with!
Thanks
Pete
 
Upvote 0
NewOrderFac33

Does it work if you move the image file to a local folder?
 
Upvote 0
NewOrderFac33

Does it work if you move the image file to a local folder?
No, it doesn't, sadly. I've been reading something about resetting IE settings as Outlook picks stuff up from there, but our work permissions prevent me from doing this.
Ah well, Insert-Picture it's going to have to be, then!
 
Upvote 0

Forum statistics

Threads
1,213,556
Messages
6,114,284
Members
448,562
Latest member
Flashbond

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top