Use VSTO to add mathematical formulas to Word documents

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.