Search code examples
htmlexcelvbaoutlookjpeg

How to attach an HTML jpeg to email?


I utilize Excel to mass mail clients. My VBA code can't locate the HTML jpeg for the advertisement.

Is the imgur link not good or is it my code?

MailWindows()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range

    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")

    On Error GoTo cleanup
    For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" Then

            Set OutMail = OutApp.CreateItem(0)
    
            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = "insert here"
.Attachments.Add Fname, 1, 0
                .HTMLBody = "<img src=""cid:"https://url.jpeg height=1650 width=1275>"
                .Display  
            End With
            On Error GoTo 0
            Set OutMail = Nothing
        End If
    Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

Solution

  • Besides the quote problems, you are referring to an image through cid (content-id), which means the email client must look for an attachment in the same message with the Content-ID MIME header set to https://url.jpeg.

    If you meant to refer to an external image, it needs to be

    .HTMLBody = "<img src=""https://url.com/someimage.jpeg"" height=1650 width=1275>"
    

    If you refer to an embedded image, you need to actually add the attachment and set PR_ATTACH_CONTENT_ID MAPI property appropriately using Attachment.PropertyAccessor.SetProperty