Paste from Word and Outlook
Text copied from Word or Outlook often includes Office-specific HTML as well as the visible formatting. Automatic Word-paste detection cleans that markup before insertion. Enable the cleanup as shown below, then use Pasting if your application needs additional rules.
Pasted content from Microsoft Word carries a lot of Word-specific markup: MsoNormal classes, <o:p> namespace tags, conditional comments aimed at older Internet Explorer, and a long list of mso-* style properties. Left in place, this noise can break downstream consumers such as PDF exporters (collapsed headings, wrong bullet spacing, fonts changing mid-paragraph).
WpfHtmlEditor auto-detects Word content on the clipboard and strips this noise before inserting it. Set MsIePasteBehavior to false so the editor runs its paste pipeline. Keep AutoDetectWordPaste enabled to recognize and clean Word markup:
// Use the editor paste pipeline, then enable Word detection.
editor.Options.MsIePasteBehavior = false;
editor.Options.AutoDetectWordPaste = true;' Use the editor paste pipeline, then enable Word detection.
editor.Options.MsIePasteBehavior = False
editor.Options.AutoDetectWordPaste = True
You may need cleanup beyond the automatic Word-noise removal, such as enforcing a house style (a single font and text color) or capping the size of a paste. Subscribe to the Pasting event: it fires after the built-in Word cleanup has run and gives you the already-cleaned HTML, so you can strip anything else you do not want, or cancel the paste outright.
Use e.IsPastingFromMsWord to apply extra rules only to Word-sourced pastes; pastes from plain text editors, IDEs, or web pages pass through untouched. The example below strips inline color and font-family styles and cancels pastes larger than 200 KB:
public MainWindow()
{
InitializeComponent();
editor.Pasting += Editor_Pasting;
}
private void Editor_Pasting(object sender, PastingHtmlEventArgs e)
{
if (e.IsPastingFromMsWord && !string.IsNullOrEmpty(e.PastingHtml))
{
// House style: drop inline colors and font-family choices.
e.PastingHtml = Regex.Replace(e.PastingHtml, @"(?<![-\w])(?:color)\s*:\s*(?:[^;'""<>{}]|""[^""';<>{}]*""|'[^'"";<>{}]*')*;?", "");
e.PastingHtml = Regex.Replace(e.PastingHtml, @"(?<![-\w])(?:font-family)\s*:\s*(?:[^;'""<>{}]|""[^""';<>{}]*""|'[^'"";<>{}]*')*;?", "");
}
if (e.PastingHtml != null && e.PastingHtml.Length > 200_000)
e.Cancel = true;
}Public Sub New()
InitializeComponent()
AddHandler editor.Pasting, AddressOf Editor_Pasting
End Sub
Private Sub Editor_Pasting(sender As Object, e As PastingHtmlEventArgs)
If e.IsPastingFromMsWord AndAlso Not String.IsNullOrEmpty(e.PastingHtml) Then
' House style: drop inline colors and font-family choices.
e.PastingHtml = Regex.Replace(e.PastingHtml, "(?<![-\w])(?:color)\s*:\s*(?:[^;'""<>{}]|""[^""';<>{}]*""|'[^'"";<>{}]*')*;?", "")
e.PastingHtml = Regex.Replace(e.PastingHtml, "(?<![-\w])(?:font-family)\s*:\s*(?:[^;'""<>{}]|""[^""';<>{}]*""|'[^'"";<>{}]*')*;?", "")
End If
If e.PastingHtml IsNot Nothing AndAlso e.PastingHtml.Length > 200_000 Then e.Cancel = True
End Sub
The IsPastingFromMsWord flag on the event args identifies which pastes came from Word, so you can apply cleanup rules selectively. Pastes from regular text editors, IDEs, or web pages pass through untouched.
The toolbar also includes a dedicated "Paste from Word" button. Clicking it runs the Word cleanup unconditionally, even if the clipboard content is not auto-detected as Word-flavored. Use this button whenever you are not sure of the paste source.
Ask your AI to do this
Connect the SpiceLogic MCP server, then give your coding assistant this prompt.
I use the SpiceLogic WPF HTML Editor. Help me set MsIePasteBehavior to false, enable AutoDetectWordPaste and handle Pasting for additional cleanup. Keep unrelated pastes unchanged. Confirm the exact API names, types and defaults with the SpiceLogic MCP documentation, then show the changes for my project.