none
how to insert equation programmatilly using c#? RRS feed

Answers

  • Hi ISE Student,

    Your request is supported. You can refer to this page for more information about contextual ribbon tab.

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by ISE Student Sunday, September 9, 2012 5:37 PM
    Sunday, September 9, 2012 6:40 AM
    Moderator

All replies

  • hi everyone,

    I want to make a shape for my equation editor like the shape of the word equation which we got it by this code

    Word.WdOMathFunctionType.wdOMathFunctionBox

    by using c#

    how can we do it??

    Thursday, September 6, 2012 8:40 AM
  • Hi ISE,

    Thank you for posting in the MSDN Forum.

    The code below show how to use OMathBox. It's an Console Application.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace ConsoleApplication_OMatchFunctionBox
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Suppose you have a document in Directory D:\ named Doc_1.docx.
                string fName = @"D:\Doc_1.docx";
    
                Word._Application myApp = new Word.Application();
                myApp.Visible = true;
    
                Word.Document myDoc = myApp.Documents.Open(fName);
    
                //The below code add a wdOMathFunctionBox to the document opened.
                Word.Range myFunctionR = myApp.Selection.OMaths.Add(myApp.Selection.Range);
                Word.OMathFunction myFunction = myApp.Selection.OMaths[1].Functions.Add(
                    myApp.Selection.Range, Word.WdOMathFunctionType.wdOMathFunctionBox);
                Word.OMathBox myBox = myFunction.Box;
                
                //Since OMathBox.E property returns an OMath object that represents the base
                //of the specified equation object. You can then refer to OMath members' page
                //to see what methods and properties can be used. The below show how to assign
                //a string to the range of myBox.
                myBox.E.Range.Text = "Hello World!";
    
            }
        }
    }
    

    Effect of the code:

    Reference links used to accomplish the code above:
    http://msdn.microsoft.com/en-us/library/ff846176.aspx
    http://msdn.microsoft.com/en-us/library/ff846196.aspx
    http://msdn.microsoft.com/en-us/library/ff841702

    Hope it helps. If there's anything not clear, please feel free to let me know.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, September 7, 2012 7:05 AM
    Moderator
  • thanx alot.

    but could I change the design tab content from eqaution symbols to be addins tab content??

    thanx again

    Sunday, September 9, 2012 5:24 AM
  • Hi ISE Student,

    Your request is supported. You can refer to this page for more information about contextual ribbon tab.

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by ISE Student Sunday, September 9, 2012 5:37 PM
    Sunday, September 9, 2012 6:40 AM
    Moderator
  • thanx alot

    can I change the alignment of characters in the box which we got it from this code

    Word.OMathBox myBox = myFunction.Box;
    ??

    when I write  arabic characters in this box  the first letter I wrote it appears at the last order.

    for ex:

    if I wrote  a letter   then b letter in sin function

    it appears

    sin b a 

    can I solve this problem for arabic characters????

    thanx again and sorry if my english language is not good :)

    Sunday, September 9, 2012 5:18 PM
  • Hi ISE Student,

    I've record a macro when using the sin function. Macro can be seen below.

    Sub Macro9()
    '
    ' Macro9 Macro
    '
    '
        Selection.OMaths(1).Functions.Add Range:=Selection.Range, Type:= _
            wdOMathFunctionFunc
        Selection.MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdMove
        Selection.TypeText Text:="sin"
        Selection.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdMove
    End Sub

    Then I adapted the C# code into below to add a sin function to a word document.

    Code snippet 1:

    static void Main(string[] args)
    {
        //Suppose you have a document in Directory D:\ named Doc_1.docx.
        string fName = @"D:\Doc_1.docx";
    
        Word._Application myApp = new Word.Application();
        myApp.Visible = true;
    
        Word.Document myDoc = myApp.Documents.Open(fName);
    
        //The below code add a wdOMathFunctionBox to the document opened.
        Word.Range myFunctionR = myApp.Selection.OMaths.Add(myApp.Selection.Range);
        Word.OMathFunction myFunction = myApp.Selection.OMaths[1].Functions.Add(
            myApp.Selection.Range, Word.WdOMathFunctionType.wdOMathFunctionFunc);
    
        myApp.Selection.MoveLeft(Word.WdUnits.wdCharacter, 2, Word.WdMovementType.wdMove);
        myApp.Selection.TypeText("sin");
        myApp.Selection.MoveRight(Word.WdUnits.wdCharacter, 1, Word.WdMovementType.wdMove);
        myApp.Selection.TypeText("ab");
        
    }

    Effect of Code snippet 1:

    Or else you can try the code below, it provides you another way to add a sin function.

    Code snippet 2:

    static void Main(string[] args)
    {
        //Suppose you have a document in Directory D:\ named Doc_1.docx.
        string fName = @"D:\Doc_1.docx";
    
        Word._Application myApp = new Word.Application();
        myApp.Visible = true;
    
        Word.Document myDoc = myApp.Documents.Open(fName);
    
        //The below code add 2 wdOMathFunctionBox to the document opened.
        Word.Range myFunctionR = myApp.Selection.OMaths.Add(myApp.Selection.Range);
        Word.OMathFunction myFunction_Left = myApp.Selection.OMaths[1].Functions.Add(
            myApp.Selection.Range, Word.WdOMathFunctionType.wdOMathFunctionBox);
        Word.OMathFunction myFunction_Right = myApp.Selection.OMaths[1].Functions.Add(
            myApp.Selection.Range, Word.WdOMathFunctionType.wdOMathFunctionBox);
    
        Word.OMathBox myBoxLeft = myFunction_Left.Box;
        Word.OMathBox myBoxRight = myFunction_Right.Box;
    
        myBoxLeft.E.Range.Text = "sin";
        myBoxRight.E.Range.Text = "a"; //Letter a.
    
        Thread.Sleep(1000);//Make it easier for you to see the change.
        //Remember to add using System.Threading; at the top of your code.
    
        myBoxRight.E.Range.Text = myBoxRight.E.Range.Text + "b"; //Then it become ab.
    }

    Effect of Code snippet 2:

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us


    Monday, September 10, 2012 2:21 AM
    Moderator
  • thank you very much for your reply.

    your code help me alot but in other problems. :)

    I'm not going to insert sin function but I'm going to build my own functions in arabic characters

    for example we wrote symbol

    جا

    instead of sin symbol. this isn't my problem because I do it.

    My problem is:

    when I wrote the arabic letters in the Word.OMathBox object next to the arabic function symbol it appears left to right 

    I want it to be ordered right to left.

    also when I wrote operations like + or - .. the language changes to english.

    I tried all methods and properties but it doesn't work.

    is there a solution for this problem??!!!!! 

    how could I format the objects which user inserts them in Word.OMathBox object 

     at runtime ??

    Monday, September 10, 2012 4:26 AM
  • Hi,

    I am using word document to be created programatically using c#.
    I have to include equations in the word document e.g


    \aleph
    \alpha
    \Alpha
    \amalg
    \angle
    \approx
    \asmash
    \ast
    \asymp
    \atop
    \bar
    \Bar
    \begin
    \below
    \bet
    \beta
    \Beta
    \bot
    \bowtie
    \box


    I use the following code to make it visible in the word document

    -------------------------------------------------------------------------------------
         Application application = new Application();
                Document document = application.Documents.Open(@"C:\WorkDocument\word.doc");

                var script = @"\eta";

                var rngLatex1 = document.Range();
                rngLatex1.Text = script;

                rng = rng.OMaths.Add(rng);
                OMath objEq = rng.OMaths[1];
                rng.OMaths[1].Type = WdOMathType.wdOMathInline;
                objEq.BuildUp();
                document.Save();

    -------------------------------------------------------------------------------------

    The document created displays \eta and not the expected symbol

    PFA

    Wednesday, December 5, 2018 5:58 AM
  • You will have to identify all the Greek alphabets and replace with the equivalent UTF16 code.

    For eg.

    var script = @"\delta = (PL)/(AE)";

    script = script.Replace("\delta", "\u03B4");

    There are websites for UTF-16 codes for greek alphabets

    Also, set

    WordApplication.OMathAutoCorrect.UseOutsideOMath = true; for auto correction of math equation

    Tuesday, December 11, 2018 9:40 AM