使用 Excel VBA 在默认 Outlook 签名中显示图片

Displaying picture in default Outlook signature using Excel VBA

提问人:naur 提问时间:11/11/2023 最后编辑:Communitynaur 更新时间:11/13/2023 访问量:47

问:

下面是在指定表中创建带有附件的电子邮件的代码。我努力在这些电子邮件中保留我的默认签名,并注意到图像没有显示。

此代码将由其他用户使用,因此将其链接到默认的 Outlook 签名将是理想的选择。

Sub send_Multiple_Email()

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Bulk Email Table")

Dim OA As Object
Dim msg As Object, signature As String

Set OA = CreateObject("Outlook.Application")

Set msg = OA.createitem(0)

With msg
    .Display
    signature = msg.HTMLbody
End With

Dim i As Integer
Dim last_row As Integer

last_row = Application.WorksheetFunction.CountA(sh.Range("B:B"))

With msg

    For i = 2 To last_row
        Set msg = OA.createitem(0)
 
        msg.To = sh.Range("B" & i).Value
        msg.cc = sh.Range("C" & i).Value
        msg.Subject = sh.Range("D" & i).Value
        msg.HTMLbody ActiveSheet.TextBoxes("TextBox 3").Text & vbNewLine & signature
        msg.Attachments.Add sh.Range("E" & i).Value

        msg.Display
    Next i 

    MsgBox "Emails Prepared"

End With

Set OA = Nothing
Set msg = Nothing

End Sub

除了我的公司徽标在添加的签名中消失外,一切都按预期进行。手动起草电子邮件是可行的,但运行此代码不会显示图片。

Excel VBA 展望

评论

0赞 niton 11/11/2023
如果这些答案适用,请考虑投赞成票 使用 Excel VBA 在 Outlook 中添加由图像组成的默认签名,并将带有图像的签名添加到邮件中

答:

0赞 artodoro 11/11/2023 #1

只需将作业移动到电子邮件创建循环中即可:signature

Sub send_Multiple_Email()

  Dim sh As Worksheet
  Set sh = ThisWorkbook.Sheets("Bulk Email Table")

  Dim OA As Object
  Dim msg As Object, signature As String

  Set OA = CreateObject("Outlook.Application")

  Set msg = OA.createitem(0)

' ~> FROM THIS
'  With msg
'    .Display
'    signature = msg.HTMLbody
'  End With

  Dim i As Integer

  Dim last_row As Integer
  last_row = Application.WorksheetFunction.CountA(sh.Range("B:B"))

  With msg
    For i = 2 To last_row
      Set msg = OA.createitem(0)
' ~> TO THIS
      msg.Display
      signature = msg.HTMLbody

      msg.To = sh.Range("B" & i).Value
      msg.cc = sh.Range("C" & i).Value
      msg.Subject = sh.Range("D" & i).Value
      msg.HTMLbody ActiveSheet.TextBoxes("TextBox 3").Text & vbNewLine & signature
      msg.Attachments.Add sh.Range("E" & i).Value

    Next i

    MsgBox "Emails Prepared"

  End With

  Set OA = Nothing
  Set msg = Nothing

End Sub

评论

0赞 naur 11/11/2023
非常感谢!这就像一个魅力,感谢您的帮助!
0赞 artodoro 11/11/2023
不客气!将此消息标记为答案,谢谢)
0赞 niton 11/12/2023
为了获得更简洁的代码,请考虑注释上面的 。然后注释和 .Set msg = OA.createitem(0)' ~> FROM THISWith msgEnd With