Pasting from MS Word and Outlook into the editor
Elena is the lead developer on the email-marketing composer at a mid-market B2B platform. Her customers' marketing teams draft every campaign in Microsoft Word -- headlines, body copy, CTAs, the whole thing -- and paste finished blocks into the composer fifty times a day. The composer wraps everything in a branded email template before sending.
For two sprints in a row, support tickets piled up about emails arriving in Outlook with bizarre line spacing, oversized fonts, and gray boxes where text should have been. Elena dug in and found the culprit: raw Word HTML. Word emits MsoNormal classes, inline mso-* styles, Office XML namespaces, and conditional comments. Her template's CSS was being overridden by Word's inline styles, and the email clients rendered the mess in unpredictable ways.
She remembered that WinFormHtmlEditor has a built-in Word cleanup. She opened the property grid on her editor instance and found Options.AutoDetectWordPaste already set to true by default. So the cleanup should have been running. She wrote a small test: paste a Word document into the editor and read back DocumentHtml. The output was clean. The MsoNormal classes and mso-* styles were gone. So far so good.
[IMAGE: word-paste-auto-detect.png -- side-by-side: raw Word HTML on the left, the editor's cleaned output on the right]
The actual problem turned out to be one rogue colleague who had flipped Options.MsIePasteBehavior to true in a feature branch, thinking it would speed up paste performance. With that flag on, the editor delegates paste to the underlying browser engine and the AutoDetectWordPaste cleanup is bypassed entirely. Worse, the Pasting event hands you a null PastingHtml in that mode because the editor never sees the HTML the browser inserts. Elena reverted the flag and the dirty markup stopped escaping.
// Elena's defaults, set once during composer initialization:htmlEditor1.Options.AutoDetectWordPaste = true; // already the defaulthtmlEditor1.Options.MsIePasteBehavior = false; // already the defaultWith the bug fixed, Elena turned to the next item on her PM's list: enforce the company style guide on every pasted block. Marketing was pasting in 14pt Calibri from Word, but the email template uses 11pt system fonts. She subscribed to the Pasting event and rewrote the HTML before the editor inserted 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, @"(font-family|font-size)\s*:\s*[^;""]+;?", string.Empty, RegexOptions.IgnoreCase); // e.IsPastingFromMsWord tells Elena the source was Word-shaped, // so she can apply 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;};The event args give Elena everything she needs. PastingHtml is read/write -- replace it and the editor inserts your version. IsPastingFromMsWord is a quick flag for Word-detected content (or content arriving via the legacy BtnPasteFromMsWord toolbar button). Cancel aborts the paste entirely. IsModified is a read-only signal the editor uses to know whether the handler changed anything.
[IMAGE: pasting-event-flow.png -- diagram: clipboard -> detect Word -> cleanup -> Pasting event -> document]
One last cleanup item: the toolbar still showed the legacy "Paste from MS Word" button (BtnPasteFromMsWord). Elena's users no longer needed it because automatic detection covers the same case on the regular paste action. She hid the button to declutter the toolbar.
htmlEditor1.BtnPasteFromMsWord.Visible = false;[IMAGE: paste-from-word-toolbar-button.png -- the BtnPasteFromMsWord button highlighted on the toolbar before being hidden]
Two weeks later the support queue for paste-related bugs was empty. The marketing team noticed nothing -- which is exactly what Elena wanted.