vba 替换 HTML 正文中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24154063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:25:34  来源:igfitidea点击:

Replace text in HTML body

vbaemailoutlook

提问by user3728348

I'm trying to replace text in the body of a template already created in Outlook 2010. The purpose of this is so that users can update the contact which the email is being sent to fairly easily.

我正在尝试替换已在 Outlook 2010 中创建的模板正文中的文本。这样做的目的是让用户可以相当轻松地更新将电子邮件发送到的联系人。

Sub NewUserEmail()

Dim myItem As Outlook.MailItem
Dim strContact As String
Dim strCompanyName As String
Dim strHTML As String

Set myItem = Application.CreateItemFromTemplate( _
      "C:\Users\jim.reagan\AppData\Roaming\Microsoft\Templates\NewUserEmail.oft")
    strHTML = myItem.HTMLBody
    strContact = InputBox("What is the Contact's name?")
    myItem.HTMLBody = Replace(myItem.HTMLBody, "%<Contact>%", strContact)

myItem.Display
End Sub

The template opens up for review but no replacements have been made to the body of the email. If I use myItem.Body the replacement works but then I lose my formatting of my email. What am I missing?

该模板已打开以供审核,但未对电子邮件正文进行任何替换。如果我使用 myItem.Body 替换工作,但我丢失了我的电子邮件格式。我错过了什么?

回答by user3728348

I'm not sure how to tag or flag an answer for this but here is the code that I got to work from the editing provided by Tim Williams, thank you for your help with this:

我不确定如何为此标记或标记答案,但这是我从 Tim Williams 提供的编辑中得到的代码,感谢您对此的帮助:

Sub NewUserEmail()

    Dim myItem As Outlook.MailItem
    Dim strContact As String
    Dim strCompanyName As String
    Dim strHTML As String


    Set myItem = Application.CreateItemFromTemplate("C:\file location\file.oft")
    strHTML = myItem.HTMLBody
    strContact = InputBox("What is the Contact's name?")
    myItem.HTMLBody = Replace(myItem.HTMLBody, "%CONTACT%", strContact)

    myItem.Display

End Sub