-1

I need to embed two pictures in the email body (it doesn't matter if I attach them too).

This code attaches, but it doesn't embed.

'Attach(0)' means full path of 1st pic
'Attach(1)' means full path of 2nd pic
'Temp(0) means "shortname1.jpg"
'Temp(1) means "shortname2.jpg"

Dim Temp(1) As String
Temp(0) = Replace(Attach(0), ruta & "\", "")
Temp(1) = Replace(Attach(1), ruta & "\", "")
Set MItem = OutlookApp.CreateItem(olMailItem)
With MItem
    .To = Email
    .CC = CopyEmail
    .Subject = Localidad & " " & "- Pay Period " & Format(Semana, "DD-MMM-YYYY") & " " & "Report"
    .Attachments.Add lnvo
    .Attachments.Add Attach(0), 1
    .Attachments.Add Attach(1), 1
    .HTMLBody = "<html><p>Charts</p>" & "<img src=""cid:" & Temp(0) & """height=520 width=750>" & "<img src=""cid:" & Temp(1) & """height=520 width=750>"
    .Body = Msg
    .Send
    '
End With
Hans Felix Ramos
  • 4,264
  • 3
  • 16
  • 40
AJ Suarez
  • 43
  • 8
  • If you're embedding an image within an html email then the image needs to be attached to the email and cannot contain a path because that path won't be available at the recipient's end. – ChrisFNZ Sep 19 '19 at 21:14
  • If you are going to use double quotes inside a string, either use triple quotes or use a single quote instead... ie... ` – braX Sep 19 '19 at 21:15
  • https://stackoverflow.com/questions/44869790/embed-picture-in-outlook-mail-body-excel-vba – braX Sep 19 '19 at 21:42
  • Thank you very much for your comments. The paths are used just to attache the files (I tested it and they are attached). Now, my question goes more to "am I building the htmlBody part properly"?. I know about VB but I don't know about that html thing. – AJ Suarez Sep 19 '19 at 21:51
  • you arent specifying all of the variable values in your example, so it's hard to know for sure, but you are on the right track... `""` should work. (check the link I previously posted) (assuming `Temp(0)` is just the filename with extension and has no path in it) – braX Sep 19 '19 at 22:23
  • You're right @braX, I apologize. I had considered not relevant since everything was working good except the HTMLBody part. Not sure if one of the issues could come by an inconsistence of Body or HTMLBody. I'll let the specialists (you all LOL) to let me know if this is true! The link you posted was the one where I took the code. I had already cared about the extension and path notes. – AJ Suarez Sep 19 '19 at 22:51
  • I shared for other people in the future the final solution. Hope it help! Thank you very much! – AJ Suarez Sep 19 '19 at 22:52

1 Answers1

0

Based on @ChrisFNZ and @braX comments, going up and down, I removed '.Body' and I inserted the '.Body' Text in the 'HTMLBody' ('Msg' Variable).

In the other hand, I decided to insert the double quotes in the Temp() variables. and it worked perfectly.

    Dim Temp(1) As String
    Temp(0) = Replace(Attach(0), ruta & "\", "") & """"
    Temp(1) = Replace(Attach(1), ruta & "\", "") & """"
    Set MItem = OutlookApp.CreateItem(olMailItem)
    With MItem
        .To = Email
        .CC = CopyEmail
        .Subject = Localidad & " " & "- Pay Period " & Format(Semana, "DD-MMM-YYYY") & " " & "Report"
        .Attachments.Add lnvo
        .Attachments.Add Attach(0), 1
        .Attachments.Add Attach(1), 1
        .HTMLBody = "<html><p" & Msg & "</p>" & "<img src=""cid:" & Temp(0) & " height=150 width=750>" & "<img src=""cid:" & Temp(1) & " height=150 width=750>"
        '.Body = Msg
        .Send
    End With

Thanks everyone!

AJ Suarez
  • 43
  • 8