【问题标题】:Replace text in MailItem Body替换 MailItem 正文中的文本
【发布时间】:2017-02-17 13:58:56
【问题描述】:

我在 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 正文中的文本值,并确保现有的文本格式和图片不丢失?

【问题讨论】:

    标签: c# vsto outlook-addin


    【解决方案1】:

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

    我已经粘贴了下面的代码。 如果有人能想出一种更快、更少闪烁的方法,请告诉我。

    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);
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        下面这个替换邮件项的正确工作代码

         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;
                }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-07-31
          • 2015-12-19
          • 2014-04-26
          • 2020-08-15
          • 1970-01-01
          • 1970-01-01
          • 2021-04-04
          相关资源
          最近更新 更多