Modify HTMLBody of Outlook Email, based on Template, from Excel

拟墨画扇 提交于 2019-12-13 03:30:51

问题


I am trying to modify the HTML body of an Outlook email, based on a template, from Excel VBA.

My code is:

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

The question is very similar to this.

I get

Run Time Error 287. Application-defined or object-defined error

on the .HTMLBody modification line.

If I remove this line the email is displayed for the user to check before hitting send.

I have referenced the Microsoft Outlook 15 Object Library.

I added:

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.

It looks like from the debugger that there is no HTML body:

I have confirmed that body type is set to HTML both programmatically:

and by opening the oft message and checking:


回答1:


The cause of the issue can be related to the Substitute method, so I'd suggest running the following code to make sure everything works correctly:

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

Another aspect is Outlook security prompts. Read more about that in the "A program is trying to send an e-mail message on your behalf" warning in Outlook article.




回答2:


The most probable cause is Outlook Security.

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.

You can find the security configurations in HKCU\Software\Policies\Microsoft\office\16.0\outlook\security\ (change 16.0 to your office version)

There are two values that you can check, promptoomaddressbookaccess and promptoomaddressinformationaccess

Change them to 2 (or ask your system administrator), restart Outlook and try again.

More info https://support.microsoft.com/en-za/help/926512/information-for-administrators-about-e-mail-security-settings-in-outlo



来源:https://stackoverflow.com/questions/56998674/modify-htmlbody-of-outlook-email-based-on-template-from-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!