This is an extremely simple example to add a mathematical summary formula to a Word document.
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using Word = Microsoft. Office. Interop. Word;
Using System. Diagnostics;
Namespace VSTOInsertEquations
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
Word. Application wdApplication = null;
Process [] pl = Process. GetProcessesByName ("WINWORD.exe ");
If (pl. Length> 0)
{
WdApplication = (Word. Application) System. Runtime. InteropServices
. Marshal. GetActiveObject ("Word. Application ");
}
Else
{
WdApplication = new Word. Application ();
}
If (wdApplication! = Null)
{
Word. Document newDocument = wdApplication. Documents. Add ();
// A summary formula is added to the following code.
Word. Range wdFunctionR = wdApplication. Selection. OMaths
. Add (wdApplication. Selection. Range );
Word. OMathFunction wdFunction = wdApplication. Selection
. OMaths [1]. Functions. Add (wdApplication. Selection. Range,
Word. WdOMathFunctionType. wdOMathFunctionNary );
Word. OMathNary wdNary = wdFunction. Nary;
WdNary. Char = 8721;
WdNary. Grow = false;
WdNary. SubSupLim = false;
WdNary. HideSub = false;
WdNary. HideSup = false;
// Enter the value in the formula using the following code:
Word. Selection wdSelection = wdApplication. Selection;
Object unit = Word. WdUnits. wdCharacter;
Object lu = Word. WdUnits. wdLine;
Object count = 1;
Object tcount = 3;
WdSelection. MoveLeft (ref unit, ref count );
WdSelection. TypeText ("11 ");
WdSelection. MoveLeft (ref unit, ref tcount );
WdSelection. TypeText ("12 ");
WdSelection. MoveDown (ref lu, ref count );
WdSelection. TypeText ("13 ");
WdNary. Application. Visible = true;
}
}
}
}
From TX_OfficeDev's column