简体   繁体   English

Outlook / Word自动格式化VBA生成的电子邮件

[英]Outlook/Word Autoformats VBA Generated Email

I am trying to generate and then preview an email using an Excel macro. 我正在尝试使用Excel宏生成然后预览电子邮件。 Essentially, I am creating a new MailItem with CreateItem(0) , setting the HTMLBody property, then calling myemail.Display . 本质上,我正在使用CreateItem(0)创建一个新的MailItem ,设置HTMLBody属性,然后调用myemail.Display myemail.Display opens up the email in Outlook's inspector, which, AFAIK, first runs the email through some sort of Microsoft Word parser, which strips much of the formatting. myemail.Display在Outlook的检查myemail.Display打开电子邮件,该检查器AFAIK首先通过某种Microsoft Word解析器运行电子邮件,该解析器剥离了许多格式。 If instead of calling myemail.Display I just call myemail.Send , the email is formatted properly, but in this use case we really want to be able to preview the email first. 如果不是调用myemail.Display而是调用myemail.Send ,则电子邮件的格式正确,但是在这种情况下,我们确实希望能够首先预览电子邮件。

The particular formatting that is being stripped is that all left-aligned text is being centered. 正在剥离的特定格式是所有左对齐的文本都居中。 It's not a huge deal, but it would be good if I could understand this behavior. 这没什么大不了的,但是如果我能理解这种行为,那就太好了。 Some potential solutions/answers could include: 1. How to format the HTML so that Word doesn't strip the formatting 2. How to turn off the Word parser engine entirely 3. How to turn off Word auto centering of text 一些潜在的解决方案/答案可能包括:1.如何设置HTML格式,以使Word不剥离格式2.如何完全关闭Word解析器引擎3.如何关闭Word自动居中文本

The most promising thing I tried was making a left aligned table, however Outlook/Word added an extra ugly two empty lines after the table. 我尝试过的最有希望的事情是制作一个左对齐的表格,但是Outlook / Word在表格之后增加了一个难看的两行。 Many solutions look mostly good, but the whole point of formatting/previewing this is because the email is being sent out to important people so I want it to look as good as possible. 许多解决方案看起来大多不错,但是格式化/预览的整个要点是因为电子邮件已发送给重要人员,所以我希望它看起来尽可能好。

Here is an example where Outlook/Word will auto center this text 这是一个示例,其中Outlook / Word将自动将此文本居中

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail
   .To = "someemail@email.com"
   .BCC = ""
   .Subject = "a subjhect"
   .SentOnBehalfOfName = "anotheremail@email.com"
   .HTMLBody = "here is some text that will be auto centered"
   .Display
End With

Your .HTMLBody doesn't actually contain any HTML currently. 您的.HTMLBody当前实际上不包含任何HTML。 HTML emails are parsed differently by various rendering engines, so the actual subset of HTML you can use across the board can be a case of trial-and-error to determine. 各种渲染引擎对HTML电子邮件的解析方式有所不同,因此可以全面使用的HTML实际子集可能是反复试验的结果。

But for something like a simple left-align, it shouldn't be too involved. 但是对于像一个简单的左对齐这样的事情,就不应该太复杂。

Perhaps something like: 也许像这样:

.HTMLBody = "<html><body><p align="left">here is some text that will be auto centered</p></body></html>"

I guess my point is, the centering isn't coming from your code, except possibly by omission. 我想我的意思是,居中不是来自您的代码,除非有可能遗漏。 The lack of formatting markup is leaving it to the rendering engine to decide how best to display it. 缺少格式标记将其留给呈现引擎来决定如何最好地显示它。

Based on your subsequent comment, I'd suggest sticking with tables (they're generally the most reliable way to format HTML emails in my experience) but you may need to add some Outlook-specific CSS in your document HEAD to sort out the extra spacing: 根据您随后的评论,我建议您坚持使用表格(根据我的经验,表格通常是最可靠的格式化HTML电子邮件的方式),但是您可能需要在文档HEAD中添加一些特定于Outlook的CSS才能整理出多余的内容间距:

/* Stop Outlook from adding extra spacing to tables. */

    table,
    td {
        mso-table-lspace: 0pt !important;
        mso-table-rspace: 0pt !important;
    }

If that's helpful, you can find the above (and further tips) at ContactMonkey 如果有帮助,您可以在ContactMonkey中找到以上内容(以及更多提示)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM