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.
The WinForms HTML editor detects when pasted content originates from Microsoft Word or Outlook and strips the markup that typically breaks rendering elsewhere: MsoNormal classes, inline mso-* styles, Office XML namespaces, and conditional comments. This cleanup runs when Options.AutoDetectWordPaste is in effect. By default Options.MsIePasteBehavior is true, which routes paste through the underlying browser engine and bypasses this cleanup - see below for how to enable it. Once enabled, paste a Word document and read back DocumentHtml to confirm the output is clean of Word-specific markup.
<html xmlns:o="urn:schemas-microsoft-com:office:office"> <!--[if gte mso 9]><xml><o:OfficeDocumentSettings></o:OfficeDocumentSettings></xml><![endif]--> <p class="MsoNormal" style="margin:0in;mso-margin-top-alt:auto;font-size:14.0pt; font-family:Calibri;mso-fareast-language:EN-US">Hello <b>world</b></p> </html>
<p><b>Hello world</b></p>
The xmlns declaration, the conditional comment, the MsoNormal class, and every mso-* inline style are stripped - only the semantic markup and formatting survive.
By default, Options.MsIePasteBehavior is true: the editor delegates paste handling to the underlying browser engine, the AutoDetectWordPaste logic is bypassed entirely, and the Pasting event receives a null PastingHtml because the editor never sees the HTML the browser inserts. To get the automatic Word cleanup described above, set MsIePasteBehavior to false explicitly.
// Set once during composer initialization:
htmlEditor1.Options.AutoDetectWordPaste = true; // already the default
htmlEditor1.Options.MsIePasteBehavior = false; // must be set explicitly - the shipped default is trueTo enforce additional formatting rules on pasted content, for example stripping inline font declarations so the host application's own CSS takes over, subscribe to the Pasting event and rewrite e.PastingHtml before the editor inserts it.
htmlEditor1.Pasting += (sender, e) => {
if (string.IsNullOrEmpty(e.PastingHtml))
return;
// Strip inline font-family and font-size so the template's CSS wins.
e.PastingHtml = Regex.Replace(
e.PastingHtml,
@"(?<![-\w])(?:font-family|font-size)\s*:\s*(?:[^;'"<>{}]|"[^"';<>{}]*"|'[^'";<>{}]*')*;?",
string.Empty,
RegexOptions.IgnoreCase);
// e.IsPastingFromMsWord indicates the source was Word-shaped,
// so the handler applies Word-specific extra rules on top of the built-in cleanup.
if (e.IsPastingFromMsWord)
{
// The marketing team sometimes embeds clip-art via <v:shape>.
// Strip those - they never render in email clients anyway.
e.PastingHtml = Regex.Replace(e.PastingHtml, @"<v:[^>]*>.*?</v:[^>]*>",
string.Empty, RegexOptions.Singleline);
}
// Reject pastes that try to smuggle in <script> - security policy.
if (e.PastingHtml.IndexOf("<script", StringComparison.OrdinalIgnoreCase) >= 0)
e.Cancel = true;
};AddHandler htmlEditor1.Pasting, Sub(sender, e)
If String.IsNullOrEmpty(e.PastingHtml) Then Return
' Strip inline font-family and font-size so the template's CSS wins.
e.PastingHtml = Regex.Replace(
e.PastingHtml,
"(?<![-\w])(?:font-family|font-size)\s*:\s*(?:[^;'"<>{}]|"[^"';<>{}]*"|'[^'";<>{}]*')*;?",
String.Empty,
RegexOptions.IgnoreCase)
' e.IsPastingFromMsWord indicates the source was Word-shaped,
' so the handler applies Word-specific extra rules on top of the built-in cleanup.
If e.IsPastingFromMsWord Then
' The marketing team sometimes embeds clip-art via <v:shape>.
' Strip those - they never render in email clients anyway.
e.PastingHtml = Regex.Replace(e.PastingHtml, "<v:[^>]*>.*?</v:[^>]*>",
String.Empty, RegexOptions.Singleline)
End If
' Reject pastes that try to smuggle in <script> - security policy.
If e.PastingHtml.IndexOf("<script", StringComparison.OrdinalIgnoreCase) >= 0 Then
e.Cancel = True
End If
End SubPastingHtml is read/write - replace it and the editor inserts your version. IsPastingFromMsWord flags Word-detected content, including content arriving via the legacy BtnPasteFromMsWord toolbar button. Cancel aborts the paste entirely. IsModified is a read-only flag indicating whether the handler changed the content.
(Ctrl+V or the Paste button)
MsIePasteBehavior = true
execCommand("Paste") - built-in
Word cleanup bypassed
e.PastingHtml is forced null
document as MSHTML inserted it
(set explicitly)
the clipboard for Word/Outlook
markers (MsoNormal, mso-*, xmlns)
FilterMsWordHtmlHelper
strips the Word cruft
cleaned HTML in e.PastingHtml,
e.IsPastingFromMsWord = true
e.PastingHtml or set e.Cancel
into the document
The toolbar's legacy "Paste from MS Word" button (BtnPasteFromMsWord) is redundant once automatic Word detection is enabled, since the regular paste action already covers the same case. Hide it to simplify the toolbar:
htmlEditor1.BtnPasteFromMsWord.Visible = false;htmlEditor1.BtnPasteFromMsWord.Visible = False
Ask your AI to do this
Connect the SpiceLogic MCP server, then give your coding assistant this prompt.
I use the SpiceLogic WinForms 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.