vba 如何替换 Outlook 电子邮件正文中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20136576/
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-08 17:16:54  来源:igfitidea点击:

How to replace text in the body of an Outlook email message

vbaemailoutlook-vba

提问by Gene

I have text data that I read from a file; I need to place it in the body of an email message. There are about 50 such data items to be placed into existing text.

我有从文件中读取的文本数据;我需要将它放在电子邮件的正文中。大约有 50 个这样的数据项要放入现有文本中。

It seems I should be able to put a marker (dataItem1, dataItem2, etc.) and replace that with the matching text data from the file.

看来我应该能够放置一个标记(dataItem1、dataItem2 等)并将其替换为文件中匹配的文本数据。

Searches only turn up replacing field data such as Recipient, Subject, etc., or replacing the entire body. Would I have to generate the entire body in the code? Seems like I should be able to "insert" a data item into existing body text.

搜索只会替换字段数据,例如收件人、主题等,或替换整个正文。我是否必须在代码中生成整个身体?似乎我应该能够将数据项“插入”到现有的正文中。

Any suggestions will be much appreciated.

任何建议将不胜感激。

回答by Dr Phil

Just in case your email is formatted as HTML, and you don't want to lose the formatting, here's what you want to do.

以防万一您的电子邮件格式为 HTML,并且您不想丢失格式,这就是您想要做的。

myMessage.HTMLBody = Replace(myMessage.HTMLBody, "dataItem1", "replacement item")

回答by Mike

Use the Bodyproperty of the MailItemyou're working with. Assign it to a variable and you can edit it:

使用您正在使用的Body属性MailItem。将其分配给一个变量,您可以对其进行编辑:

Dim body As String

body = myMessage.Body
body = Replace(body,"dataItem1","your replacement here")

Then assign the variable to the Body:

然后将变量分配给Body

myMessage.Body = body