如何使用替换功能保持格式?

How to keep formatting with replace function?

我搜索了网站的 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

您的 HTML 正文将没有“<”和“>”文本字符(如 "<ProjectName>"),它们将被 HTML 编码 - &lt;&gt;

Outlook 使用 Word 作为电子邮件编辑器。您可以使用Word 对象模型对邮件正文进行修改,至少WOM 提供了一种方便的替换文本片段的方法。为了让事情正常进行,您需要使用 Inspector class 的 WordEditor 属性,其中 returns 文档 class 的一个实例Word 对象模型。您可以在 Chapter 17: Working with Item Bodies(包括示例代码)中找到所有可能的使用项目主体的方法。