Add picture into body of email

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Add picture into body of email

Post by gailb »

Hello All,

I'm trying to add an .jpg to the body of this email, but the picture is not showing up.

It says, "The linked image cannot be displayed." I stepped thru the code and I know the path and name are good.

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add picture into body of email

Post by HansV »

Since you're using code, could you post it - or at least the relevant part of it?
Best wishes,
Hans

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Add picture into body of email

Post by gailb »

Oops, sorry, I thought I had.

Code: Select all

Sub SendMail()
    
    Dim Fname As String: Fname = "C:\Users\gb\Desktop\HappyBirthday.jpg"
    Dim MyEmail As MailItem: Set MyEmail = Application.CreateItem(olMailItem)
    
    With MyEmail
        .To = ""
        .Subject = "Happy Birthday"
        .HTMLBody = "Dear," & _
                    "<p>" & "Wishing you all the best." & _
                    "<p>" & "<img src=""cid:HappyBirthday.jpg""height=360 width=520>"

        .BodyFormat = olFormatHTML
        .Display
    End With
    
End Sub

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add picture into body of email

Post by HansV »

Try this:

Code: Select all

Sub SendMail()
    Dim Fname As String: Fname = "C:\Users\gb\Desktop\HappyBirthday.jpg"
    Dim MyEmail As MailItem: Set MyEmail = Application.CreateItem(olMailItem)
    Dim doc As Object ' Word document
    Dim rng As Object ' Word range
    
    With MyEmail
        .To = ""
        .Subject = "Happy Birthday"
        .BodyFormat = olFormatHTML
        .Display
        Set doc = .GetInspector.WordEditor
        doc.Content.InsertAfter "Dear,"
        doc.Content.InsertParagraphAfter
        doc.Content.InsertAfter "Wishing you all the best."
        doc.Content.InsertParagraphAfter
        Set rng = doc.Content
        rng.Collapse Direction:=0 ' wdCollapseEnd
        rng.InlineShapes.AddPicture FileName:=Fname
    End With
End Sub
Best wishes,
Hans

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Add picture into body of email

Post by gailb »

Thank you Hans. This works great.