title: Replace text in MailItem Body
body: <p>I've added a ribbon button in the Outlook Explorer, that creates a new email from the selected email when clicked. This works fine using the MailItem.Copy method. But I also need to replace some text in the message body with a different value.</p>
  
  <p>The problem is the email could be a HTML/RichText formatted email and contain text formatting and/or pictures. And simply replacing the text value in the Body property loses all the text formatting and pictures.</p>
  
  <p>So the code below is no good</p>
  
  <pre><code>newMailItem.Body = newMailItem.Body.Replace("Old Value", "New Value");
  </code></pre>
  
  <p>And I've also tried loading the HTML and RTF value into a DevExpress RichEditControl and used the RichEditControl.Document.ReplaceAll method to try and replace the text occurrences. But the DevExpress RichEditControl changes/formats the RTF / HTML value differently and causes the message to look wrong when the HTML / RTF is set back in the MailItem.</p>
  
  <p>I've also tried replacing the text by getting a reference to the Word Document (see the code below). But this doesn't work either.</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>So the question is, how can I replace a text value in the MailItem body and ensure the existing text formatting and pictures aren't lost?</p>