0

I am working with VBA through Access to create mail.

When I adjust an image size to less than the actual size, it embeds. When I omit size or put in as actual size, the image comes as an attachment.

Below is code snippet, miniplane.jpg comes as an attachment. Its actual size is 600x160. If I change in the code to 300x80 it shows.

Sub send_SHCmail()

    '>>>> Declarations >>>>

    Dim strPath As String
    Dim strFileName As String
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)

    Dim olApp As Object
    Dim objMail As Object

    Set SHCData1 = Nothing
    Set SHCData2 = Nothing

    '>>>> Email Creation and Outlook Error Loop >>>>

    On Error Resume Next 'Keep going if there is an error
    Set olApp = GetObject(, "Outlook.Application") 'See if Outlook is open

    If Err Then 'Outlook is not open
       Set olApp = CreateObject("Outlook.Application") 'Create a new instance
    End If

    'Create e-mail item
    Set objMail = olApp.CreateItem(olMailItem)
    With objMail

        'Set body format to HTML
        .BodyFormat = olFormathtml
        .To = "no-reply@email.com"
        .Bcc = SHCDistribution
        .Sentonbehalfofname = "test@email.com"
        .Subject = "Planning Report - " & Format(Now, "MMMM d, yyyy")
        .Attachments.Add "\\local\Sdata\Logo-Facet-01.png", olByValue, 0
        .Attachments.Add "\\local\Sdata\miniplane.jpg", olByValue, 0
        .HTMLBody = "<!DOCTYPE html>"

        'Body Header
        .HTMLBody = .HTMLBody & "<html><head><body>"
        .HTMLBody = .HTMLBody & "<img src=""cid:Logo-Facet-01.png"" alt=""Image Missing"" width=""215.6"" height=""96.53"" style=""display: block;"" />"
        .HTMLBody = .HTMLBody & "<img src=""cid:miniplane.jpg"" alt=""Image Missing"" width=""600"" height=""160"" style=""display: block;"" />"

        .Display
    End With

End Sub
Erik A
  • 31,639
  • 12
  • 42
  • 67

1 Answers1

0

Firstly, don't use HTMLBody as an intermediate variable - reading and setting it is expensive. Use a local variable to build HTML , then set HTMLBody property only once.

If you are setting img tag through the src attribute, you must set the PR_ATTACH_CONTENT_ID property - see How to add images from resources folder as attachment and embed into outlook mail body in C#

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78