您的位置:首页 > 其它

使用VSTO向Word文档中添加数学公式

2012-02-09 12:49 387 查看
这是个极其简单的实例,用来向Word文档中添加一个数学汇总公式。

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();
//一下代码添加了汇总公式
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;
//以下代码将数值填写入公式
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;
}
}
}
}


相关资源:http://download.csdn.net/detail/tx_officedev/4053367
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: