C#打开模板替换指定书签内容并生成word文档

发布时间:2010年07月12日      浏览次数:1191 次
1、 环境
Windows 2003 server
Office 2003
Vs2005
.net framework 2.0
2、 步骤
1、 新建模版保存到E:\word\1.dot;
2、 在新建的模版中添加入书签demo到指定的位置;
3、 新建windows 应用程序demo;
4、 在程序中添加引用Microsoft Word 11.0 Object Library;
5、 编写程序;
3、 代码
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
加入上面两条using语句
添加一个textbox名为:demo
添加一个按钮,在其事件中写入:
object oMissing = System.Reflection.Missing.Value;
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
object oTemplate = "E:\word\1.dot";
oWord = new Word.Application();
oWord.Visible = false;
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
ref oMissing, ref oMissing);
object oBookMark = "demo";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = this.demo.Text.Trim();
object oFilename = "E:\demo\1.doc";
oDoc.SaveAs(ref oFilename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Close(ref oMissing,ref oMissing,ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
this.Close();
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!