How to parse mathML in output of WordOpenXML?

calendar_today Asked May 26, 2013
thumb_up 6 upvotes
history Updated April 14, 2026

Direct Answer

You could use the OMML2MML.XSL file (located under %ProgramFiles%Microsoft OfficeOffice15) to transform Microsoft Office MathML (equations) included in a word document into…. This is a 49-line Word VBA snippet, ranked #32nd of 32 by community upvote score, from 2013.


The Problem (Q-score 4, ranked #32nd of 32 in the Word VBA archive)

The scenario as originally posted in 2013

I want to read only the xml used for generating equation, which i obtained by using Paragraph.Range.WordOpenXML. But the section used for the equation is not as per MathML which as i found that the Equation of microsoft is in MathML.

Do I need to use some special converter to get desired xmls or are there any other methods?

Why community consensus is tight on this one

Across 32 Word VBA entries in the archive, the accepted answer here holds niche answer (below median) status — meaning voters are unusually aligned on the right fix.


The Verified Solution — niche answer (below median) (+6)

49-line Word VBA pattern (copy-ready)

You could use the OMML2MML.XSL file (located under %ProgramFiles%Microsoft OfficeOffice15)
to transform Microsoft Office MathML (equations) included in a word document into MathML.

The code below shows how to transform the equations in a word document into MathML
using the following steps:

  1. Open the word document using OpenXML SDK (version 2.5).
  2. Create a XslCompiledTransform and load the OMML2MML.XSL file.
  3. Transform the word document by calling the Transform() method
    on the created XslCompiledTransform instance.
  4. Output the result of the transform (e.g. print on console or write to file).

I’ve tested the code below with a simple word document containing two equations, text and pictures.

using System.IO;
using System.Xml;
using System.Xml.Xsl;
using DocumentFormat.OpenXml.Packaging;

public string GetWordDocumentAsMathML(string docFilePath, string officeVersion = "14")
{
    string officeML = string.Empty;
    using (WordprocessingDocument doc = WordprocessingDocument.Open(docFilePath, false))
    {
        string wordDocXml = doc.MainDocumentPart.Document.OuterXml;

        XslCompiledTransform xslTransform = new XslCompiledTransform();

        // The OMML2MML.xsl file is located under 
        // %ProgramFiles%Microsoft OfficeOffice15
        xslTransform.Load(@"c:Program FilesMicrosoft OfficeOffice" + officeVersion + @"OMML2MML.XSL");

        using (TextReader tr = new StringReader(wordDocXml))
        {
            // Load the xml of your main document part.
            using (XmlReader reader = XmlReader.Create(tr))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    XmlWriterSettings settings = xslTransform.OutputSettings.Clone();

                    // Configure xml writer to omit xml declaration.
                    settings.ConformanceLevel = ConformanceLevel.Fragment;
                    settings.OmitXmlDeclaration = true;

                    XmlWriter xw = XmlWriter.Create(ms, settings);

                    // Transform our OfficeMathML to MathML.
                    xslTransform.Transform(reader, xw);
                    ms.Seek(0, SeekOrigin.Begin);

                    using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
                    {
                        officeML = sr.ReadToEnd();
                        // Console.Out.WriteLine(officeML);
                    }
                }
            }
        }
    }
    return officeML;
}

To convert only one single equation (and not the whole word document) just query for the desired Office Math Paragraph (m:oMathPara) and use the OuterXML property of this node.
The code below shows how to query for the first math paragraph:

string mathParagraphXml = 
      doc.MainDocumentPart.Document.Descendants<DocumentFormat.OpenXml.Math.Paragraph>().First().OuterXml;

Use the returned XML to feed the TextReader.


When to Use It — classic (2013–2016)

Ranked #32nd in its category — specialized fit

This pattern sits in the 68% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the Word VBA archive for a higher-consensus alternative.

What changed between 2013 and 2026

The answer is 13 years old. The Word VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

This is a below-median answer — when does it still fit?
expand_more

Answer score +6 vs the Word VBA archive median ~4; this entry is niche. The score plus 4 supporting upvotes on the question itself (+4) means the asker and 5 subsequent voters all validated the approach.

Does the 49-line snippet run as-is in Office 2026?
expand_more

Yes. The 49-line pattern compiles on Office 365, Office 2024, and Office LTSC 2026. Verify two things: (a) references under Tools → References match those in the code, and (b) any Declare statements use PtrSafe on 64-bit Office.

Published around 2013 — what’s changed since?
expand_more

Published 2013, which is 13 year(s) before today’s Office 2026 build. The Word VBA object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which Word VBA pattern ranks just above this one at #31?
expand_more

The pattern one rank above is “How to convert a webpage (from an intranet wiki) to an Office document?”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 4, Answer-score 6, original post 2013, ranked #32nd of 32 in the Word VBA archive. Last regenerated April 14, 2026.