Sending, receiving, and organizing email in Outlook on the web for business
The behavior is most likely caused by how Outlook/Word parses and “cleans up” HTML for email, especially around lists, inline styles, and post‑processing of the HtmlBody.
Key points from the provided information:
- Outlook uses Word as the HTML editor
- When an email is displayed or sent, Word’s HTML engine validates and rewrites the HTML.
- This can cause:
- Loss of CSS‑based formatting
- Changes to paragraph and list structure
- Reassignment of styles to
MSONormaland similar classes
- Any complex or non‑Word‑friendly HTML that is injected and then re‑edited in Outlook can be reordered or reformatted.
- Editing
HtmlBodydirectly is fragile- When HTML is set programmatically and then the item is displayed/sent, Outlook/Word may strip or rearrange formatting.
- The recommended approach is to work with the underlying Word editor object instead of repeatedly manipulating
HtmlBody.
- Lists and line breaks are especially vulnerable
- Bullet and numbered lists, nested lists, and extra line breaks are common points where Outlook/Word “fixes” the HTML.
- Cleanup logic like:
- Moving nodes around
ul/olandli - Injecting
<br>after certain tags - Collapsing spaces and
can easily create HTML that Word normalizes differently, leading to content appearing out of order when the draft is reopened or edited.
- Moving nodes around
How this relates to the current implementation
- The
ensureHtmlFormatfunction:- Parses and rewrites the entire HTML tree.
- Moves nodes around list items (
ol > li), wraps text nodes into<p>or<li>, and injects inline styles. - Performs regex replacements on the final HTML string (adding
<br>, collapsing spaces, removing extra ).
- After this, Outlook/Word applies its own normalization, which can:
- Merge or split paragraphs and list items.
- Reorder or unwrap elements that don’t match its expected structure.
- Remove or change inline styles.
- The combination of aggressive DOM manipulation plus Outlook’s own HTML rewrite is what causes content to “lose its position” or get shuffled when editing the draft.
Recommended direction for a more stable solution
- Avoid heavy post‑processing of
HtmlBody- Minimize structural changes to lists and paragraphs.
- Avoid regex‑based manipulation of the final HTML where possible, especially around
<p>,<br>,<ul>,<ol>, and<li>.
- Use the Word editor object instead of repeatedly editing
HtmlBody- For Outlook items created via the Outlook Object Model, use the underlying
WordEditorof the inspector to edit HTML/RTF content. - This aligns the content with Word’s internal model from the start, reducing the chance that Word will later “fix” or reorder it.
- For Outlook items created via the Outlook Object Model, use the underlying
- Keep list markup simple and Word‑friendly
- Use standard
<ul>,<ol>, and<li>without deep nesting or complex inline styles. - Avoid wrapping lists in multiple nested
<div>/<font>elements or adding non‑standard spacing constructs that Word may normalize.
- Use standard
- Test with minimal transformations
- Start from a simple, static HTML template that renders correctly in Outlook.
- Gradually add only the transformations that are strictly necessary, checking after each change whether Outlook still preserves order when the draft is edited.
There is no single API call that guarantees Outlook will never reorder HTML, but using the Word editor object instead of repeatedly rewriting HtmlBody, and keeping the HTML structure simple and Word‑compatible, is the documented workaround to prevent formatting and layout from being lost or shuffled.
References: