替换 MailItem 正文中的文本
Replace text in MailItem Body
我在 Outlook 资源管理器中添加了一个功能区按钮,单击它会从选定的电子邮件中创建一封新电子邮件。使用 MailItem.Copy 方法效果很好。但我还需要用不同的值替换邮件正文中的一些文本。
问题是电子邮件可能是 HTML/RichText 格式的电子邮件并包含文本格式 and/or 图片。简单地替换 Body 属性 中的文本值会丢失所有文本格式和图片。
所以下面的代码不行
newMailItem.Body = newMailItem.Body.Replace("Old Value", "New Value");
我还尝试将 HTML 和 RTF 值加载到 DevExpress RichEditControl 中,并使用 RichEditControl.Document.ReplaceAll 方法尝试替换出现的文本。但是 DevExpress RichEditControl changes/formats RTF / HTML 值不同,当 HTML / RTF 在 MailItem 中重新设置时导致消息看起来错误。
我还尝试通过获取对 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 正文中的文本值并确保现有的文本格式和图片不丢失?
我通过显示检查器 window 使其正常工作,因为这似乎在执行查找和替换后将 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);
}
您可以改用 HTMLBody。它保持格式和更新顺利。
Globals.ThisAddIn.mailItem.HTMLBody = Globals.ThisAddIn.mailItem.HTMLBody.Replace(oldVal, newVal);
为这个 Replace Mail 项目更正下面的工作代码
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;
}
}
我在 Outlook 资源管理器中添加了一个功能区按钮,单击它会从选定的电子邮件中创建一封新电子邮件。使用 MailItem.Copy 方法效果很好。但我还需要用不同的值替换邮件正文中的一些文本。
问题是电子邮件可能是 HTML/RichText 格式的电子邮件并包含文本格式 and/or 图片。简单地替换 Body 属性 中的文本值会丢失所有文本格式和图片。
所以下面的代码不行
newMailItem.Body = newMailItem.Body.Replace("Old Value", "New Value");
我还尝试将 HTML 和 RTF 值加载到 DevExpress RichEditControl 中,并使用 RichEditControl.Document.ReplaceAll 方法尝试替换出现的文本。但是 DevExpress RichEditControl changes/formats RTF / HTML 值不同,当 HTML / RTF 在 MailItem 中重新设置时导致消息看起来错误。
我还尝试通过获取对 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 正文中的文本值并确保现有的文本格式和图片不丢失?
我通过显示检查器 window 使其正常工作,因为这似乎在执行查找和替换后将 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);
}
您可以改用 HTMLBody。它保持格式和更新顺利。
Globals.ThisAddIn.mailItem.HTMLBody = Globals.ThisAddIn.mailItem.HTMLBody.Replace(oldVal, newVal);
为这个 Replace Mail 项目更正下面的工作代码
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;
}
}