如何使用VBA将图像嵌入到Outlook电子邮件中 [英] How to embed an image into an Outlook email using VBA

查看:118
本文介绍了如何使用VBA将图像嵌入到Outlook电子邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嵌入紧密相关Outlook邮件正文excel vba中的图片

我正在尝试将图像嵌入到Outlook电子邮件中.

I'm trying to embed an image into an Outlook email.

我正在使用以下代码段,其中一半是从上面的帖子中窃取的:

I'm using the following code snippet, half of which has been stolen from the post above:

Sub PictureEmail()
    Dim outApp As New Outlook.Application
    Dim OutMail As Object
    Dim Attchmnt As String
    Dim Signature As String
    Dim WB As Workbook
    Set WB = ThisWorkbook
   Attchmnt = "C:\Users\Blah\Painted_Lady_Migration.jpg"


    Set OutMail = outApp.CreateItem(0)
    On Error Resume Next
    With OutMail
    .To = WB.Names("to").RefersToRange.Value2
    .CC = WB.Names("cc").RefersToRange.Value2
    .BCC = WB.Names("bcc").RefersToRange.Value2
    .Subject = WB.Names("Subject").RefersToRange.Value2
   .HTMLBody = "<img src=""cid:Painted_Lady_Migration.jpg""height=520 width=750>"
   .display
   End With

   If Attchmnt = "" Then
   Else
   OutMail.Attachments.Add Attchmnt
   End If

   On Error GoTo 0

End Sub

但是,当查看生成的电子邮件时,出现错误无法显示链接的图像.该文件可能已被移动,重命名或删除".

However, when looking at the generated email, I have the error "The linked image cannot be displayed. The file may have been moved, renamed, or deleted".

我尝试了几种附加文件的方法,包括:

I've tried a few different ways to attach the file, including:

.HTMLBody = "<img src=" & Chr(34) & "cid:Painted_Lady_Migration.jpg" & Chr(34) & "height=520 width=750>"

我只是无法使它工作> _<

I just can't get it to work >_<

我在某处看到名称/文件路径中的空格会引发该错误,所以我用下划线替换了该名称中的空格

I saw somewhere that spaces in the name/filepath can throw it, so I replaced the spaces in the name with underscores

我忘记/错过了什么愚蠢的事情?

What dumb thing am I forgetting/missing?

推荐答案

cid 是在附加时创建的,因此需要在显示/发送它之前执行此操作.

The cid is created when you attach it, so you need to do that before you display/send it.

尝试一下

Set OutMail = outApp.CreateItem(0)
With OutMail
    .To = WB.Names("to").RefersToRange.Value2
    .CC = WB.Names("cc").RefersToRange.Value2
    .BCC = WB.Names("bcc").RefersToRange.Value2
    .Subject = WB.Names("Subject").RefersToRange.Value2
    If Attchmnt <> "" Then 
       .Attachments.Add Attchmnt ' (additional arguments are optional)
       .HTMLBody = "<img src=""cid:Painted_Lady_Migration.jpg"" height=520 width=750>"
    Else
       .HTMLBody = "[no attachment included]"
    End If
   .Display
End With

这篇关于如何使用VBA将图像嵌入到Outlook电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆