【问题标题】:Modify HTMLBody of Outlook Email, based on Template, from Excel从 Excel 修改 Outlook 电子邮件的 HTMLBody,基于模板
【发布时间】:2019-11-21 17:34:05
【问题描述】:

我正在尝试根据 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非常相似。

我明白了

运行时错误 287。应用程序定义或对象定义的错误

在 .HTMLBody 修改行上。

如果我删除此行,则会显示电子邮件供用户在点击发送之前检查。

我已经引用了 Microsoft Outlook 15 对象库。

我补充说:

With OutMail
    .bodyFormat = olFormatHTML

但是在 Substitute 行上出现了同样的错误,所以我将替代更改为:

.HTMLBody = "<HTML><BODY>Some HTML text here</BODY></HTML>"

并且电子邮件的正文已更新。

所以只有在尝试使用替代品时才会出现错误。

从调试器看来,没有 HTML 正文:

我已确认以编程方式将正文类型设置为 HTML:

并通过打开消息并检查:

【问题讨论】:

  • 当 VBA 有一个内置方法可以这样做时,为什么要尝试使用工作表函数来进行替换? OutMail.HTMLBody = Replace(OutMail.HTMLBody, "String to find", "Replacement string")
  • @RyszardJędraszyk 然而并没有解决这个问题。
  • 如果是 Outlook 安全错误(我认为可能是这样),有没有办法将 HTML 正文存储为文件,单独修改然后将其添加到正文中?
  • OutMail 是 HTML 格式的邮件吗?
  • @TimWilliams 是的。

标签: excel vba outlook


【解决方案1】:

问题的原因可能与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 安全提示。在"A program is trying to send an e-mail message on your behalf" warning in Outlook 文章中了解更多信息。

【讨论】:

    【解决方案2】:

    最可能的原因是 Outlook 安全性。

    出于安全考虑,HTMLBody、HTMLEditor、Body 和 WordEditor 属性都受地址信息安全提示的约束,因为邮件正文通常包含发件人或其他人的电子邮件地址。

    您可以在 HKCU\Software\Policies\Microsoft\office\16.0\outlook\security\ 中找到安全配置 (将 16.0 更改为您的 office 版本)

    有两个值可以查看,promptoomaddressbookaccess 和promptoomaddressinformationaccess

    将它们更改为 2(或询问您的系统管理员),重新启动 Outlook 并重试。

    更多信息https://support.microsoft.com/en-za/help/926512/information-for-administrators-about-e-mail-security-settings-in-outlo

    【讨论】:

      猜你喜欢
      • 2021-08-10
      • 2016-07-12
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多