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. The cleanup is on by default; 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 on every normal paste while Options.AutoDetectWordPaste is on, which is the default, and Office content always goes through it whatever Options.MsIePasteBehavior is set to. 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.
Options.MsIePasteBehavior (true by default on WinForms) affects only content that does not come from Office. While it is true, such content is pasted directly by the editing surface, and the Pasting event receives a null PastingHtml for it. To run every paste through your Pasting handler, set it to false:
// Set once during composer initialization:
htmlEditor1.Options.AutoDetectWordPaste = true; // already the default
htmlEditor1.Options.MsIePasteBehavior = false; // every paste reaches the Pasting eventTo 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)
Content from Word, Outlook
or another Office app
(on by default)
headings, highlights, tables
and pictures kept
cleaned HTML in e.PastingHtml,
e.IsPastingFromMsWord = true
e.PastingHtml or set e.Cancel
into the document
MsIePasteBehavior = true:
pasted directly, Pasting event
gets a null e.PastingHtml
Pasting event gets the HTML
to rewrite or 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.