vbaimageemail

VBA Email image not showing in received email


I have a user form that I fill in with information. A button on the form sends an email confirmation to an address in from a text box. It all works well, but the image does not show in the body text and the attachment is visible. Don't want that.

Public Sub CommandButton8_Click() ' Send confirmation email

 Dim outApp As Object
    Dim OutMail As Object
    Dim Attchmnt, strbody As String
    Dim WB As Workbook
    Set WB = ThisWorkbook
    Set outApp = CreateObject("Outlook.Application")
    Set OutMail = outApp.CreateItem(0)
    
    Attchmnt = "c:\Users\bradb\OneDrive\Desktop\SWC-Logo.png"

    strbody = "<BODY style='font-size:18pt;font-family:Calibri'>" & "Hello " & Registration.TextBox2.Value & ", <p> " & _
                "Please verify your squad selection and average division,<br>" & _
                "And please reply with correct information if an error is found.<p>" & _
                "Squad: " & Registration.ComboBox1.Value & " <br> " & _
                "Average: " & Registration.TextBox9.Value & " <br> " & _
                "Division: " & Registration.TextBox10.Value & " <p> " & _
                "Thank You." & " <br> "
   
    Set OutMail = outApp.CreateItem(0)
    With OutMail
        .To = Registration.TextBox7.Value
        .CC = ""
        .BCC = ""
        .Subject = "Tournament Confirmation"
        .Attachments.Add Attchmnt, 1, 0
        .HTMLBody = strbody & "<img src=""cid:SWC-Logo.png"" height=69 width=216> <br> " & _
        "Brad Bylls <br> " & _
        "Tournament Manager" & .HTMLBody
        
        OutMail.Send
        MsgBox "eMail sent"
        
    End With
End Sub

Pic of received email


Solution

  • Here is a simplified version which shows the basic steps from the linked answer by John at https://stackoverflow.com/a/27545610/478884

    Public Sub TestMailWithInlineImage() 
        'use Const for fixed values
        Const ATT_PATH As String = "C:\Temp\sig.png"
        Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
        
        With CreateObject("Outlook.Application").CreateItem(0)
            .To = "you@gmail.com"
            .CC = ""
            .BCC = ""
            .Subject = "Inline image test"
    
            '### this is the key part: set a content-id for the attachment ###
            With .Attachments.Add(ATT_PATH, 1, 0)
                .PropertyAccessor.SetProperty PR_ATTACH_CONTENT_ID, "SWC-Logo.png"
            End With
            '### done setting content-id ###
    
            'Then add the body
            .HTMLBody = "<p>This is the first line</p>" & _
                        "<p><img src='cid:SWC-Logo.png' height=69 width=216></p>" & _
                        "<p>This is the second line</p>"
            .send
            
            MsgBox "eMail sent"
        End With
    End Sub