JLA JLA -4 years ago 201
C# Question

Microsoft.Office.Interop.Word OMath

this is my first question on StackOverflow, so have patience :D

I'm actually trying to generate .docx files from .txt files throught Microsoft.Office.Interop.Word lib in Visual Studio Environment (I'm coding in C#).

Those Word files are meant to be exercices for children with specific help (colors, size, etc...).

Some of the txt files contains equations that I would like to recreate on Word with the specifi tool "OMath" (that, if I understood, will be used to create those "equation" boxes).

At that time, I didn't succeeded in generating more than 1 box at a time. For example, if a txt has 2 or more equations, all of them are created in the same equation box but not separately.

I have tried a code like that :

using Word = Microsoft.Office.Interop.Word;


Word.Application wapp = new Word.Application();
Word.Document wdoc = wapp.Documents.Add();


wpara = wdoc.Paragraphs.Add();
e.ElementAspect.setFormat(ref wpara); // set wpara format (color, font ...)
Word.Range test1 = wapp.Selection.Range;
test1.Text = e.Content;
test1 = wpara.Range.OMaths.Add(test1);
Word.OMath equation = test1.OMaths[1];

Answer Source

First of all, you don't need to use wapp.Selection object. Selection is used for current cursor/user location in document (in short) - Selection interface

Secondly, using wpara = wdoc.Paragraphs.Add() from what I have tested, gives you paragraph in current selection.

This code (as you encountered) will overwrite in each iteration prevoius equation.

for (int i = 1; i <= 10; i++)
    var para = oDoc.Paragraphs.Add();
    var paraRange = para.Range;
    paraRange.Text = "x^2 + " + i + " = y";     

And lastly, you can add multiple ranges to OMaths object, and use BuildUp to build all at once.

Try this code and combine it with your solution:

var oWord = new Word.Application();
oWord.Visible = true;                       
var oDoc = oWord.Documents.Add();
var math = oDoc.OMaths;

for (int i = 1; i <= 10; i++)
    var para = oDoc.Paragraphs[i];
    var paraRange = para.Range;
    paraRange.Text = "x^2 + " + i + " = y";


As @JLA discovered, more complex scenerios, like \mapsto or does not work.

First attempt: word macro

Word under the scenes manages to convert it so in developer ribbon tab i have enebled recording, just to notice that when recording is enabled it doesn't work anymore...

Second attempt: Converting LaTeX to MathML

From question Tools for converting LaTeX equations to Content MathML or OpenMath? I got SnuggleTeX with live demo converting to MathML but it only works for very simple scenarios like:

<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mn>1</mn><mn>2</mn></mfrac></math>

Code for adding to Word:

Clipboard.SetText("<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mrow><mn>1</mn><mn>x</mn></mrow></math>", TextDataFormat.Text);

Third attempt: convert it yourself

Change all your special names that does not work with OMath to UTF-16 chars. For example:

\mapsto = \u27FC

Code for Word:

paraRange.Text = "x^2 + "+ i + '\u27FC' +" y";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download