使用替换功能和.HTMLBody失去格式的Outlook VBA无法正常工作

时间:2015-04-15 20:50:37

标签: outlook outlook-vba

我冒昧地搜索了该站点的Outlook VBA堆栈溢出部分,但找不到与此问题相关的任何内容。我有一个我创建的模板。当我打开模板时,格式化已经是我想要的,并且签名会在消息的末尾自动填充。我创建了一个宏,提示输入以下信息,然后使用我在输入框中输入的信息替换消息中的标签。

使用谷歌(很多)后,我尝试在我的替换功能中使用myItem.Body。当我这样做时,宏工作,但我失去了所有模板和签名格式。然后我尝试在我的替换函数中使用myItem.HTMLBody,它不会替换正文中的任何内容,签名也会消失。

以下代码......我们将非常感谢任何指导。 (Outlook 2010)

Sub PopulateTemplate()
Dim myItem As MailItem, oInspector As Inspector
Set oInspector = Application.ActiveWindow
Set myItem = oInspector.CurrentItem

'Provide the email address(es) and populate in the To field
strEmailAddr = InputBox("Enter email address(es), separated by semicolon(;)")
myItem.To = Replace(myItem.To, "<EmailAddr>", strEmailAddr)
myItem.Display

'Provide the project name and populate in subject and body of email
strProjName = InputBox("Enter the Project Name")
myItem.Subject = Replace(myItem.Subject, "<ProjectName>", strProjName)
myItem.Body = Replace(myItem.Body, "<ProjectName>", strProjName)
myItem.Display

'Provide the time of day for the greeting and populate in the body of email
strTimeofDay = InputBox("Enter Morning, Afternoon, or Evening")
myItem.Body = Replace(myItem.Body, "<TimeofDay>", strTimeofDay)
myItem.Display

'Provide the first name for the greeting and populate in body of email
strContactName = InputBox("Enter the First Name for the Greeting")
myItem.Body = Replace(myItem.Body, "<ContactName>", strContactName)
myItem.Display

'Resolve all recipients (Same as pressing the "Check Names" button)
Call myItem.Recipients.ResolveAll

'Free memory
Set strEmailAddr = Nothing
Set strProjName = Nothing
Set strTimeofDay = Nothing
Set strContactName = Nothing
End Sub

2 个答案:

答案 0 :(得分:1)

你的HTML正文没有“&lt;”和“&gt;”文字字符(如"<ProjectName>")中所示,它们将采用HTML编码 - &lt;&gt;

答案 1 :(得分:0)

Outlook使用Word作为电子邮件编辑器。您可以使用Word对象模型在邮件正文中进行修改,至少WOM提供了替换文本片段的便捷方法。为了使工作正常,您需要使用Inspector类的 WordEditor 属性,该属性从Word对象模型返回Document类的实例。您可以找到使用Chapter 17: Working with Item Bodies中描述的项目主体的所有可能方法(包括示例代码)。