vba Body.Replace 去除 html 格式

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

Body.Replace strips out html formatting

vbaoutlook-vba

提问by tom redfern

I have an email template which has html formatting and place holders to swap out with real values.

我有一个电子邮件模板,它具有 html 格式和占位符,可以用实际值换出。

In Excel I load the email via the Outlook CreateItemFromTemplate method. If at this point I save the email formatting is preserved.

在 Excel 中,我通过 Outlook CreateItemFromTemplate 方法加载电子邮件。如果此时我保存电子邮件格式将被保留。

If I perform a replace on the body most of the formatting is stripped out:

如果我对正文执行替换,则大部分格式都会被删除:

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItemFromTemplate("template.oft") ' <- has lots of html formatting

With OutMail
    .Body = Replace(.Body, "#recipient#", "Some other value") ' <- Strips out most formatting!!
    .Save ' <- this works fine without the line above.
End With

回答by tom redfern

Thanks to this post : https://stackoverflow.com/a/8473313/569662

感谢这篇文章:https: //stackoverflow.com/a/8473313/569662

My problem was you have to use .HTMLBodyrather than .Body:

我的问题是你必须使用.HTMLBody而不是.Body

.HTMLBody = Replace(.HTMLBody, "#recipient#", "Some other value")