C#窗体打开word-替换标签内容

来源:互联网 发布:北方数据 杂志 编辑:程序博客网 时间:2024/05/18 01:23

http://download.csdn.net/detail/small5e4444/3845421



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;//这个在VS2008中没找到引用,是从别的地方找到的dll,我把整个程序传到了资源里


namespace word_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
             Word.ApplicationClass WordApp = new Word.ApplicationClass();


            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                object fileName = openFileDialog1.FileName;
                object readOnly = false;
                object isVisible = true;
                object missing = System.Reflection.Missing.Value;


               
                WordApp.Visible = true;


               Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);


                aDoc.Activate();


                WordApp.Selection.TypeText("Copyright C# Corner"); //给文档添加标题
                WordApp.Selection.TypeParagraph();
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            Word.ApplicationClass WordApp = new Word.ApplicationClass();


            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                count++;
                object fileName = openFileDialog1.FileName;
                object readOnly = false;
                object isVisible = true;
                object missing = System.Reflection.Missing.Value;


                WordApp.Visible = true;
             
                Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);


                aDoc.Activate();


                //WordApp.Selection.TypeText("Copyright C# Corner"); 给文档添加标题
                WordApp.Selection.TypeParagraph();






                object markName = "A";//书签标记
                Bookmark bm = aDoc.Bookmarks.get_Item(ref markName);
                bm.Range.Text = "nnnnn";//替换的内容


                markName = "B";//书签标记
                bm = aDoc.Bookmarks.get_Item(ref markName);
                bm.Range.Text = "bbbbb";//替换的内容
            }
        }
    }
}
原创粉丝点击