http://download.youkuaiyun.com/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";//替换的内容
}
}
}
}
这篇博客介绍了如何使用C#编程语言通过窗体按钮触发操作,打开Word文档并实现内容替换。通过引用dll文件和特定class,开发者可以便捷地进行Word文档处理。
350

被折叠的 条评论
为什么被折叠?



