請問各位大大
利用vba自動寄送信的簽名檔圖片,收件人無法正確顯示,請問有什麼解決方法嗎?
您可以在 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: 協議。
這樣試試
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