使用Excel VBA发送预先撰写的电子邮件以包含嵌入式图像

时间:2019-06-18 18:36:36

标签: excel vba

我预先编写了一封电子邮件,并将其另存为Outlook模板(.oft文件)。我想使用打开并发送电子邮件的excel VBA编写宏。一切正常,除了我的电子邮件是通过未格式化的邮件发送的,并且没有嵌入邮件正文的图片。图片所在的几个空白处!救命?

Sub sendemail()

''Variable Definitions

''path = the location where the already typed email is stored
Dim path As String
path = "B:\GMM-B\October.oft"

''objMsg = the varible used to store my already written and saved email message
Dim objOutlook As Outlook.Application
Dim objMsg As Outlook.MailItem

Set objOutlook = CreateObject("Outlook.Application")
Set objMsg = objOutlook.CreateItem(olMailItem)
objMsg.HTMLBody = objMsg.HTMLBody

''EmailTo = Email address list read from excel file

For i = 2 To 200
EmailTo = Sheet1.Cells(i, 3) & "; " & Sheet1.Cells(i, 4) & "; " & Sheet1.Cells(i, 5) & "; " & Sheet1.Cells(i, 2)

With Outlook.Application
    Set objMsg = CreateItemFromTemplate(path)
    objMsg.To = EmailTo
    objMsg.Subject = "Your Request"
    objMsg.BodyFormat = olFormatHTML
    objMsg.Send

End With

Next i

End Sub

2 个答案:

答案 0 :(得分:0)

我无法重现该问题(即使Outlook默认为纯文本,只要模板以HTML开头即可),但进行了一些更改:

Dim path As String, EmailTo As String
Dim objOutlook As Outlook.Application
Dim objMsg As Outlook.MailItem

path = "B:\GMM-B\October.oft"

Set objOutlook = CreateObject("Outlook.Application")
With objOutlook.Application
    For i = 2 To 200
        EmailTo = Sheet1.Cells(i, 3) & "; " & Sheet1.Cells(i, 4) & "; " & Sheet1.Cells(i, 5) & "; " & Sheet1.Cells(i, 2)
        Set objMsg = .CreateItemFromTemplate(path)
        objMsg.to = EmailTo
        objMsg.Subject = "Your Request"
        ' This line doesn't seem necessary if the .oft is already HTML/Rich Text
        'objMsg.BodyFormat = olFormatHTML
        objMsg.Send
    Next
End With

答案 1 :(得分:0)

我用了亚尔说的话并弄清楚了!!更改非常简单,但有效!大卫是对的,我两次设置了objMsg,但是第二次没有正确格式化为HTML。见下文!

Sub sendemail()

''Variable Definitions

''path = the location where the already typed email is stored
Dim path As String
path = "B:\GMM-B\October.oft"

''objMsg = the varible used to store my already written and saved email message
Dim objOutlook As Outlook.Application
Dim objMsg As Outlook.MailItem

Set objOutlook = CreateObject("Outlook.Application")
Set objMsg = objOutlook.CreateItem(olMailItem)
objMsg.HTMLBody = objMsg.HTMLBody

''EmailTo = Email address list read from excel file

For i = 2 To 200
EmailTo = Sheet1.Cells(i, 3) & "; " & Sheet1.Cells(i, 4) & "; " & Sheet1.Cells(i, 5) & "; " & Sheet1.Cells(i, 2)

With Outlook.Application
    Set objMsg = CreateItemFromTemplate(path)
    objMsg.HTMLBody = objMsg.HTMLBody
    objMsg.To = EmailTo
    objMsg.Subject = "Your Request"

objMsg.Send

End With

Next i

End Sub