当前位置:软件学习 > Word >>

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

这是个极其简单的实例,用来向Word文档中添加一个数学汇总公式。

[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 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; 
            } 
        } 
    } 


摘自 TX_OfficeDev的专栏
补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,