使用 Outlook 对象模型编辑 Outlook 项目的 HtmlBody 属性时,格式丢失

原始 KB 编号: 4020759

症状

假设使用 Outlook 对象模型创建新的 MailItemAppointmentItemMeetingItem 对象。 然后,将项的 HtmlBody 属性设置为一些以前创建的格式正确的 HTML 源,该源包含级联样式表 (CSS) 样式。 调用 Display 方法和 Send 方法发送项目后,由配置的 CSS 样式指定的格式可能会消失,或者段落样式可能会替换为 MSONormal 类。

原因

Microsoft Outlook 使用 Microsoft Word 作为其编辑器。 在发送项目时,Word HTML 引擎验证 HTML 源时,可能会出现格式丢失。

解决方法

建议在使用 Outlook 对象模型时,使用检查器的基础 WordEditor 对象 (来编辑 HTML 和 RTF 格式) Outlook 项目的正文,而不是编辑 HtmlBody 属性。 请参阅以下示例。

注意

有关详细信息,请参阅Word对象模型

using Outlook = Microsoft.Office.Interop.Outlook;
using Word = Microsoft.Office.Interop.Word;
namespace CreateAndEditMailItemUsingWord
{
    class Program
    {
        static void Main(string[] args)
        {
            Outlook.MailItem mailItem = (new Outlook.Application()).CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
            Word.Document wordDocument = mailItem.GetInspector.WordEditor as Word.Document;
            // Insert the text at the very beginning of the document
            // You can control fonts and formatting using the ParagraphFormat propety of the Word.Range object
            Word.Range wordRange = wordDocument.Range(0, 0);
            wordRange.Text = "Please insert your text here";
            mailItem.Display();
        }
    }
}