使用c#在单词中插入等式 [英] Insert an equation in word using c#

查看:70
本文介绍了使用c#在单词中插入等式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#向Microsoft Word添加一个等式。例如,我想添加以下内容:



 等式如下:



\sqrt {a ^ 2 + b ^ 3}



要做到这一点我用过这段代码:

 

 private void button2_Click(object sender,EventArgs e)
{object oMissing = System.Reflection.Missing.Value;             _Application oWord;            _Document oDoc;            oWord = new Microsoft.Office.Interop.Word.Application();            oWord.Visible = true;            oDoc = oWord.Documents.Add(ref oMissing,ref oMissing,                ref oMissing,ref oMissing) ;            writep(oDoc,"等式如下:",16,true);            oWord.Selection.OMaths.Add(oWord.Selection.Range);            OMathFunction E = oWord.Selection.OMaths [1] .Functions.Add(oWord.Selection.Range,                 WdOMathFunctionType.wdOMathFunctionBox);            OMathBox B = E.Box;            B.E.Range.Text = \\\sqrt {a ^ 2 + b ^ 3};        }




$


其中'writep'是一个在单词中插入段落的函数。现在我遇到了这些问题:



1-我不能在文本后插入等式。当我运行此代码时,方程式将插入页面顶部。我希望它在左边对齐的另一个段落中的文本后面插入。我用



$ 2如何在等式中写出等式\sqrt {a ^ 2 + b ^ 3}?是否有任何参考显示方程式是如何写的?



提前谢谢。

解决方案

< blockquote>

Hello MohammadSh94,


如何插入段落?你有没有选择光标到文本的末尾?


请检查以下代码是否可以在插入文本后插入等式。

 oDoc = oWord.ActiveDocument; 
// writep(oDoc,"等式如下:",16,true);
Word.Range rng = oDoc.Range(0,0);
string insertStr ="等式如下:" ;;
rng.Text = insertStr;
oDoc.Range(insertStr.Length,insertStr.Length).Select();
oWord.Selection.OMaths.Add(oWord.Selection.Range);

最好的问候,


Terry



I wanted to add an equation to Microsoft Word using C#. For example I want to add this:

 The equation is as follows:

\sqrt{a^2+b^3}

To do this I used this code:
 

private void button2_Click(object sender, EventArgs e) 
{ object oMissing = System.Reflection.Missing.Value;            _Application oWord;            _Document oDoc;            oWord = new Microsoft.Office.Interop.Word.Application();            oWord.Visible = true;            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,                ref oMissing, ref oMissing);            writep(oDoc, "The equation is as follows:", 16, true);            oWord.Selection.OMaths.Add(oWord.Selection.Range);            OMathFunction E = oWord.Selection.OMaths[1].Functions.Add(oWord.Selection.Range,                WdOMathFunctionType.wdOMathFunctionBox);            OMathBox B = E.Box;            B.E.Range.Text = \\ \sqrt{a^2+b^3};        }




Where 'writep' is a function which inserts a paragraph in word. Now I have these problems:

1- I can not insert the equation after the text. When I run this code the equation is inserted at the top of the page. I want it to be inserted right after the text in another paragraph with left alignment. I used

2- How to write the equation \sqrt{a^2+b^3} in the equation? Is there any reference showing how the equations are written?

Thanks in advance.

解决方案

Hello MohammadSh94,

How do you insert the paragraph? Have you you ever select the cursor to the end of the text?

Please check if below code could work for you to insert the equation after the insert text.

oDoc = oWord.ActiveDocument;
            //writep(oDoc, "The equation is as follows:", 16, true);
            Word.Range rng = oDoc.Range(0, 0);
            string insertStr = "The equation is as follows:";
            rng.Text = insertStr;
            oDoc.Range(insertStr.Length, insertStr.Length).Select();
            oWord.Selection.OMaths.Add(oWord.Selection.Range);

Best Regards,

Terry


这篇关于使用c#在单词中插入等式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆