iT邦幫忙

0

vba 自動寄信 簽名檔無法正常顯示圖片

  • 分享至 

  • xImage

請問各位大大

利用vba自動寄送信的簽名檔圖片,收件人無法正確顯示,請問有什麼解決方法嗎?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
JamesDoge
iT邦高手 1 級 ‧ 2023-02-26 21:23:44

您可以在 VBA 中使用 MailItem.Attachments.Add 方法將圖片作為附件加入郵件,然後在郵件主體中使用 HTMLBody 屬性以內嵌圖片的方式引用該圖片。以下是一個簡單的範例:

Sub SendEmailWithEmbeddedImage()
    Dim olApp As Outlook.Application
    Dim olMail As Outlook.MailItem
    Dim olAttachment As Outlook.Attachment
    Dim strImagePath As String
    Dim strHTMLBody As String
    
    ' Create a new Outlook MailItem object
    Set olApp = New Outlook.Application
    Set olMail = olApp.CreateItem(olMailItem)
    
    ' Set the recipient, subject, and body of the email
    olMail.To = "recipient@example.com"
    olMail.Subject = "Test Email with Embedded Image"
    
    ' Add the image as an attachment
    strImagePath = "C:\path\to\image.png"
    Set olAttachment = olMail.Attachments.Add(strImagePath, olByValue)
    
    ' Build the HTML body of the email with the embedded image
    strHTMLBody = "<html><body>"
    strHTMLBody = strHTMLBody & "<p>This email contains an embedded image:</p>"
    strHTMLBody = strHTMLBody & "<p><img src=""cid:" & olAttachment.FileName & """></p>"
    strHTMLBody = strHTMLBody & "</body></html>"
    
    ' Set the HTML body of the email
    olMail.HTMLBody = strHTMLBody
    
    ' Send the email
    olMail.Send
    
    ' Clean up objects
    Set olMail = Nothing
    Set olApp = Nothing
End Sub

在此範例中,我們先使用 Attachments.Add 方法將圖片作為附件加入郵件,然後在 HTML 主體中使用 標籤以 cid: 協議內嵌該圖片。在 標籤的 src 屬性中,我們使用附件的檔名來設置 cid: 協議。

joy036 iT邦研究生 3 級 ‧ 2023-02-27 09:37:25 檢舉

請問在Dim olAttachment As Outlook.Attachment出現編譯錯誤:使用者自訂型態未定義,是什麼原因? 我的版本是O365有關嗎?

0
blanksoul12
iT邦研究生 5 級 ‧ 2023-02-27 11:36:45

這樣試試

Sub SendEmailWithEmbeddedImage()

    ' Create a new Outlook MailItem object
    Set olApp = CreateObject("Outlook.Application")
    Set olApt = olApp.CreateItem(olAppointmentItem)
    Set olNewMail = olApp.CreateItem(0)
    
    ' Set the recipient, subject, and body of the email
    olNewMail.To = "recipient@example.com"
    olNewMail.Subject = "Test Email with Embedded Image"
    
    ' Add the image as an attachment
    olNewMail.Attachments.Add "C:\path\to\image.png"
    
    ' Build the HTML body of the email with the embedded image
    strHTMLBody = "<html><body>"
    strHTMLBody = strHTMLBody & "<p>This email contains an embedded image:</p>"
    strHTMLBody = strHTMLBody & "<p><img src=""cid:" & "image.png" & """></p>"
    strHTMLBody = strHTMLBody & "</body></html>"
    
    ' Set the HTML body of the email
    olNewMail.HTMLBody = strHTMLBody
    
    'display the email
    olNewMail.Display
    
    'send the email
    olNewMail.send
    
    ' Clean up objects
    Set olApp = Nothing
    Set olApt = Nothing
    Set olNewMail = Nothing
End Sub

我要發表回答

立即登入回答