从Excel修改基于模板的Outlook Email的HTMLBody [英] Modify HTMLBody of Outlook Email, based on Template, from Excel
问题描述
我正在尝试根据模板从Excel VBA修改Outlook电子邮件的HTML正文.
I am trying to modify the HTML body of an Outlook email, based on a template, from Excel VBA.
我的代码是:
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
问题与此非常相似.
我知道
运行时错误287.应用程序定义或对象定义的错误
Run Time Error 287. Application-defined or object-defined error
.HTMLBody修改行上.
on the .HTMLBody modification line.
如果我删除此行,则会显示电子邮件,供用户在点击发送之前进行检查.
If I remove this line the email is displayed for the user to check before hitting send.
我已经引用了Microsoft Outlook 15对象库.
I have referenced the Microsoft Outlook 15 Object Library.
我添加了:
With OutMail
.bodyFormat = olFormatHTML
但是在替换行上出现了相同的错误,因此我将替换项更改为:
But got the same error on the Substitute line so I changed the substitute to:
.HTMLBody = "<HTML><BODY>Some HTML text here</BODY></HTML>"
电子邮件的正文已更新.
And the body of the email was updated.
因此,该错误仅在尝试使用替代品或与之相关的情况时出现.
So the error is only present when trying to use substitute or its to do with the oft.
在调试器中似乎没有HTML正文:
It looks like from the debugger that there is no HTML body:
我已经通过编程确认了将正文类型都设置为HTML:
I have confirmed that body type is set to HTML both programmatically:
,然后打开关闭"消息并检查:
and by opening the oft message and checking:
推荐答案
最可能的原因是Outlook安全性.
The most probable cause is Outlook Security.
出于安全目的,HTMLBody,HTMLEditor,Body和WordEditor属性均受地址信息安全提示的约束,因为邮件的正文通常包含发件人或其他人的电子邮件地址.
For security purposes, the HTMLBody, HTMLEditor, Body and WordEditor properties all are subject to address-information security prompts because the body of a message often contains the sender's or other people's e-mail addresses.
您可以在HKCU \ Software \ Policies \ Microsoft \ office \ 16.0 \ outlook \ security \中找到安全配置 (将16.0更改为您的办公版本)
You can find the security configurations in HKCU\Software\Policies\Microsoft\office\16.0\outlook\security\ (change 16.0 to your office version)
您可以检查两个值,promptoomaddressaddressbookaccess和hintingoomaddressinformationaccess
There are two values that you can check, promptoomaddressbookaccess and promptoomaddressinformationaccess
将它们更改为2(或询问系统管理员),重新启动Outlook,然后重试.
Change them to 2 (or ask your system administrator), restart Outlook and try again.
这篇关于从Excel修改基于模板的Outlook Email的HTMLBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!