我正在尝试根据模板从 Excel VBA 修改 Outlook 电子邮件的 HTML 正文。
我的代码是:
Sub Email_Button()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItemFromTemplate("S:\some\path\to\file\Email.oft")
With OutMail
.Importance = olImportanceHigh
.Subject = "Subject " & Date
.Attachments.Add Application.ActiveWorkbook.FullName
.HTMLBody = WorksheetFunction.Substitute(OutMail.HTMLBody, "%target%", "replacement")
.Display
End With
' *** TIDY UP ***
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
这个问题非常类似于this https://stackoverflow.com/questions/46733209/edit-outlook-email-from-excel-vba.
I get
运行时错误 287。应用程序定义或对象定义的错误
在 .HTMLBody 修改行上。
如果我删除此行,则会显示电子邮件,供用户在点击发送之前检查。
我引用了 Microsoft Outlook 15 对象库。
I added:
With OutMail
.bodyFormat = olFormatHTML
但在替代品行上遇到了同样的错误,所以我将替代品更改为:
.HTMLBody = "<HTML><BODY>Some HTML text here</BODY></HTML>"
电子邮件正文已更新。
因此,仅当尝试使用替代品或其与经常有关时才会出现错误。
从调试器看来,没有 HTML 主体:
我已确认正文类型以编程方式设置为 HTML:
并通过打开经常消息并检查:
问题的原因可能与Substitute
方法,所以我建议运行以下代码以确保一切正常:
Sub CreateHTMLMail()
Dim OutApp As Outlook.Application
Set OutApp = CreateObject("Outlook.Application")
'Creates a new email item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create email item
Set objMail = OutApp.CreateItemFromTemplate("S:\some\path\to\file\Email.oft")
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>Enter the message text here. </BODY></HTML>"
.Display
End With
End Sub
另一个方面是Outlook安全提示。阅读有关此内容的更多信息Outlook 中出现“某个程序正在尝试代表您发送电子邮件”警告 https://support.microsoft.com/en-us/help/3189806/a-program-is-trying-to-send-an-e-mail-message-on-your-behalf-warning-i文章。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)