embedded Images in html email from Excel VBA

2019-08-08 10:14发布

I'm trying to build an automatic email that has both text and images (chart from excel) using the HTML code. It's the first time I'm writing this kind of code so I looked up and merge a few code I found online. This is what I have so far for the image:

FirstChartPath = ThisWorkbook.Path & "\Current Credit Usage.png"
    ChartName = "Current Credit Usage.png"
      'add the image in hidden manner, position at 0 will make it hidden
    .Attachments.Add FirstChartPath, olByValue, 0

    'Now add it to the Html body using image name

    'change the src property to 'cid:your image filename'
    'it will be changed to the correct cid when its sent.
    .HTMLBody = .HTMLBody & "<br><B>CURRENT CREDIT USAGE:</B><br>" _
                & "<img src='cid:Current Credit Usage.png'" & "width='500' height='200'>"
                '& "<br>Best Regards, <br>Sumit</font></span>"

The problem I'm getting is the "X" in the image saying the linked image cannot be displayed

"The file may have been moved, renamed or deleted. Verify that the link points to the correct file and location"

Can anyone spot the error?

1条回答
Emotional °昔
2楼-- · 2019-08-08 10:43

changed it to:

'Second part with the charts
    FirstChartPath = ThisWorkbook.Path & "\Current Credit Usage.jpg"
    ChartName = "Current Credit Usage.jpg"
      'add the image in hidden manner, position at 0 will make it hidden
    .Attachments.Add FirstChartPath, olByValue, 0

    'Now add it to the Html body using image name

    'change the src property to 'cid:your image filename'
    'it will be changed to the correct cid when its sent.
    .HTMLBody = .HTMLBody & "<br><B>CURRENT CREDIT USAGE:</B><br>" _
                & "<img src='" & FirstChartPath & "'width='500' height='200'>"
                '& "<br>Best Regards, <br>Sumit</font></span>"

and worked, i was missing one " ' "

查看更多
登录 后发表回答