【问题标题】:How to embed pictures in an Outlook Email using Excel VBA?如何使用 Excel VBA 在 Outlook 电子邮件中嵌入图片?
【发布时间】:2019-09-19 21:10:29
【问题描述】:

我需要在邮件正文中嵌入两张图片(如果我也附上它们也没关系)。

此代码已附加,但未嵌入。

'Attach(0)' means full path of 1st pic
'Attach(1)' means full path of 2nd pic
'Temp(0) means "shortname1.jpg"
'Temp(1) means "shortname2.jpg"

Dim Temp(1) As String
Temp(0) = Replace(Attach(0), ruta & "\", "")
Temp(1) = Replace(Attach(1), ruta & "\", "")
Set MItem = OutlookApp.CreateItem(olMailItem)
With MItem
    .To = Email
    .CC = CopyEmail
    .Subject = Localidad & " " & "- Pay Period " & Format(Semana, "DD-MMM-YYYY") & " " & "Report"
    .Attachments.Add lnvo
    .Attachments.Add Attach(0), 1
    .Attachments.Add Attach(1), 1
    .HTMLBody = "<html><p>Charts</p>" & "<img src=""cid:" & Temp(0) & """height=520 width=750>" & "<img src=""cid:" & Temp(1) & """height=520 width=750>"
    .Body = Msg
    .Send
    '
End With

【问题讨论】:

  • 如果您在 html 电子邮件中嵌入图像,则该图像需要附加到电子邮件中并且不能包含路径,因为该路径在收件人端不可用。
  • 如果你要在字符串中使用双引号,要么使用三引号,要么使用单引号...即...&lt;img src='cid:" &amp; Temp(0) &amp; "' height
  • 非常感谢您的 cmets。这些路径仅用于附加文件(我对其进行了测试并且已附加)。现在,我的问题更多地转向“我是否正确构建了 htmlBody 部分”?我知道 VB,但我不知道那个 html 的东西。
  • 您没有在示例中指定所有变量值,因此很难确定,但您走在正确的轨道上..."&lt;img src=cid:" &amp; Temp(0) &amp; "&gt;" 应该可以工作。 (查看我之前发布的链接)(假设Temp(0) 只是带​​有扩展名的文件名,其中没有路径)

标签: excel vba outlook html-email


【解决方案1】:

基于@ChrisFNZ 和@braX cmets,上下移动,我删除了“.Body”并将“.Body”文本插入到“HTMLBody”(“Msg”变量)中。

另一方面,我决定在 Temp() 变量中插入双引号。而且效果很好。

    Dim Temp(1) As String
    Temp(0) = Replace(Attach(0), ruta & "\", "") & """"
    Temp(1) = Replace(Attach(1), ruta & "\", "") & """"
    Set MItem = OutlookApp.CreateItem(olMailItem)
    With MItem
        .To = Email
        .CC = CopyEmail
        .Subject = Localidad & " " & "- Pay Period " & Format(Semana, "DD-MMM-YYYY") & " " & "Report"
        .Attachments.Add lnvo
        .Attachments.Add Attach(0), 1
        .Attachments.Add Attach(1), 1
        .HTMLBody = "<html><p" & Msg & "</p>" & "<img src=""cid:" & Temp(0) & " height=150 width=750>" & "<img src=""cid:" & Temp(1) & " height=150 width=750>"
        '.Body = Msg
        .Send
    End With

谢谢大家!

【讨论】:

    猜你喜欢
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 2014-11-07
    • 2011-12-07
    • 2017-01-09
    相关资源
    最近更新 更多