none
Insert a mathematical formula in to a word document programmatically RRS feed

  • Question

  • I want to insert a formula into a word document programmatically through my C# application. The formula needs to be entered as a text. How can I do it using the Interop.Word? Is there any way of adding it through the text fields?

    Can anyone help me out please.

    Thanks in advance,

    Vinodh.

    Tuesday, February 7, 2012 9:31 AM

Answers

  • Hi Vinodh,

    Thanks for posting in the MSDN Forum.

    I will show you a sample which will add a nary function into a document. This is a simple sample, if you want to know more about equations  please take a look OMathFunction interface in the library. I deveolped this sample under VSTO 4.0, Visual Studio 2010, Office 2010:

    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 button1_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();
                    //Following code will add a Nary Equation
                    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;
                    //Following code will setup value in Nary Function
                    Word.Selection wdSelection = wdApplication.Selection;
                    object unit = Word.WdUnits.wdCharacter;
                    object lu = Word.WdUnits.wdLine;
                    object count = 1;
                    object dcount = 2;
                    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;
                }
            }
        }
    }

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by VinodhPreetham Sunday, February 12, 2012 5:19 PM
    Thursday, February 9, 2012 3:16 AM
    Moderator

All replies

  • Hi Vinodh,

    Thanks for posting in the MSDN Forum.

    I will show you a sample which will add a nary function into a document. This is a simple sample, if you want to know more about equations  please take a look OMathFunction interface in the library. I deveolped this sample under VSTO 4.0, Visual Studio 2010, Office 2010:

    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 button1_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();
                    //Following code will add a Nary Equation
                    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;
                    //Following code will setup value in Nary Function
                    Word.Selection wdSelection = wdApplication.Selection;
                    object unit = Word.WdUnits.wdCharacter;
                    object lu = Word.WdUnits.wdLine;
                    object count = 1;
                    object dcount = 2;
                    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;
                }
            }
        }
    }

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by VinodhPreetham Sunday, February 12, 2012 5:19 PM
    Thursday, February 9, 2012 3:16 AM
    Moderator
  • Thanks Tom.

    It helped me alot.

    Thanks,

    Vinodh.


    Vinodh

    Sunday, February 12, 2012 5:20 PM
  • If I want to display my Custom Ribbon Group after inserting the equation symbol how can I do that??

    your code display the home bar of microsoft.

    I want to display my add-ins bar.

    plz help..

    thanx

    Monday, September 3, 2012 2:49 PM