替换MailItem Body中的文本

时间:2015-05-14 08:07:57

标签: c# vsto outlook-addin

我在Outlook资源管理器中添加了一个功能区按钮,用于在单击时从所选电子邮件中创建新电子邮件。使用MailItem.Copy方法可以正常工作。但我还需要用不同的值替换邮件正文中的一些文本。

问题是电子邮件可能是HTML / RichText格式的电子邮件,包含文本格式和/或图片。只需替换Body属性中的文本值就会丢失所有文本格式和图片。

所以下面的代码不好

newMailItem.Body = newMailItem.Body.Replace("Old Value", "New Value");

我还尝试将HTML和RTF值加载到DevExpress RichEditControl中,并使用RichEditControl.Document.ReplaceAll方法尝试替换文本实例。但是,DevExpress RichEditControl以不同方式更改/格式化RTF / HTML值,并且当在MailItem中重新设置HTML / RTF时,该消息会显示错误。

我还尝试通过获取Word文档的引用来替换文本(请参阅下面的代码)。但这也不起作用。

Inspector inspector = newMailItem.GetInspector;
if (inspector.IsWordMail())
{
    Microsoft.Office.Interop.Word.Document wordDocument =
        inspector.WordEditor as Microsoft.Office.Interop.Word.Document;

    Microsoft.Office.Interop.Word.Find findObject = wordDocument.Application.Selection.Find;
    findObject.ClearFormatting();
    findObject.Text = "old value";
    findObject.Replacement.ClearFormatting();
    findObject.Replacement.Text = "new value";

    object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}

所以问题是,如何替换MailItem主体中的文本值并确保现有的文本格式和图片不会丢失?

3 个答案:

答案 0 :(得分:1)

我通过显示Inspector窗口让它工作,因为这似乎在执行查找和替换后使用Word文档重新同步MailItem.Body和RTF / HTML属性。但它的速度很慢,而且很闪烁。

我已粘贴下面的代码。 如果有人能想到更快,更少闪烁的方式,请告诉我。

Inspector inspector = newMailItem.GetInspector;
if (inspector.IsWordMail())
{
    newMailItem.Display();
    wordDocument = inspector.WordEditor as Microsoft.Office.Interop.Word.Document;

    Microsoft.Office.Interop.Word.Range range = wordDocument.Range(wordDocument.Content.Start, wordDocument.Content.End);
    Microsoft.Office.Interop.Word.Find findObject = range.Find;
    findObject.ClearFormatting();
    findObject.Text = "old value";
    findObject.Replacement.ClearFormatting();
    findObject.Replacement.Text = "new value";

    object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}

答案 1 :(得分:1)

您可以使用HTMLBody代替。它可以保持格式化和更新顺畅。

    Globals.ThisAddIn.mailItem.HTMLBody = Globals.ThisAddIn.mailItem.HTMLBody.Replace(oldVal, newVal);

答案 2 :(得分:1)

更正此替换邮件项目的工作代码

 public static void Replace(Microsoft.Office.Interop.Word.Range rng, string OldValue, object NewValue)
 {
        object missing = System.Reflection.Missing.Value;
        try
        {
            Find findObject = rng.Find;
            findObject.ClearFormatting();
            findObject.Text = OldValue;
            findObject.Replacement.ClearFormatting();
            findObject.Format = true;

            object replaceAll = WdReplace.wdReplaceAll;
            findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, NewValue,
               ref replaceAll, ref missing, ref missing, ref missing, ref missing);
        }
        catch (System.Exception e)
        {
            throw e;
        }
}