C#操作Word (2)-- 打开&关闭Word文档本文正式开始在VS2010中使用C#语言操作Word2007.
不是十分了解Word对象模型的朋友,请参考上一篇文章,或者下载:C#操作Word2007.pdf。
----------------------------------华丽分割--------------------------------------------
1.添加Reference,添加命名空间
新建一个Winform工程后,首先需要给工程添加Reference
由于我的Word是2007的,所以我选择了 Microsoft Word 12.0 Object Library,
添加完成后,在Reference表单中应该多出如下两个条目:
Microsoft.Office.Core
Microsoft.Office.InterOP.Word
--------------------------------------------------------------------------
下面就正式开始编写C#代码了挖。
首先,在你的Form1.cs中添加Word命名空间:我添加的是:
[csharp]view plaincopy- usingMSWord=Microsoft.Office.Interop.Word;
2.打开Word文档
然后给Form添加一个Load事件的消息响应函数OnLoad:
好了。下一步,我们完善OnLoad函数:
[csharp]view plaincopy- PRivateMSWord.applicationm_word;
- privateMSWord.Documentm_doc;
- publicForm1()
- {
- InitializeComponent();
- }
- privatevoidOnLoad(objectsender,EventArgse)
- {
- m_word=newMSWord.Application();
- }
在OnLoad中我们实例化了一个Word的Application对象,代表Word2007应用程序。
这样只打开了一个应用程序的空壳,里面还没有文档。下面我们就打开一个已有的文档吧。
在打开之前,我们先添加一个按钮,然后为其设置Click事件的监听器OnOpen()
[csharp]view plaincopy- privatevoidOnOpen(objectsender,EventArgse)
- {
- Objectfilename="test.docx";
- Objectfilefullname=@"C:/Users/David_ss/Desktop/项目管理/test.docx";
- ObjectconfirmConversions=Type.Missing;
- ObjectreadOnly=Type.Missing;
- ObjectaddToRecentFiles=Type.Missing;
- ObjectpasswordDocument=Type.Missing;
- ObjectpasswordTemplate=Type.Missing;
- Objectrevert=Type.Missing;
- ObjectwritePasswordDocument=Type.Missing;
- ObjectwritePasswordTemplate=Type.Missing;
- Objectformat=Type.Missing;
- Objectencoding=Type.Missing;
- Objectvisible=Type.Missing;
- ObjectopenConflictDocument=Type.Missing;
- ObjectopenAndRepair=Type.Missing;
- ObjectdocumentDirection=Type.Missing;
- ObjectnoEncodingDialog=Type.Missing;
- for(inti=1;i<=m_word.Documents.Count;i++)
- {
- Stringstr=m_word.Documents[i].FullName.ToString();
- if(str==filefullname.ToString())
- {
- MessageBox.Show("请勿重复打开该文档");
- return;
- }
- }
- try
- {
- m_word.Documents.Open(reffilefullname,
- refconfirmConversions,refreadOnly,refaddToRecentFiles,
- refpasswordDocument,refpasswordTemplate,refrevert,
- refwritePasswordDocument,refwritePasswordTemplate,
- refformat,refencoding,refvisible,refopenConflictDocument,
- refopenAndRepair,refdocumentDirection,refnoEncodingDialog
- );
- m_word.Visible=true;
- //MessageBox.Show(m_word.Documents.Count.ToString());
- //MessageBox.Show(m_word.Documents[1].FullName.ToString());
- }
- catch(System.Exceptionex)
- {
- MessageBox.Show("打开Word文档出错");
- }
- }
可以看到,这里调用的是Documents对象的Open方法,参数很多,感兴趣的朋友可以参考MSDN。
上面代码中我直接写出了文件路径,当然更好的方法是弹出对话框然后选择文件。
代码中也检查了该文档是否已经打开,这样也就避免了重复打开同一文档两次。另外需要注意的是,Word应用程序打开文档的数量是从1开始数的(即1 based),不是常见的从0开始,这点需要注意一下:
for(inti=1;i<m_word.Documents.Count;i++)
在Open方法调用完成后,别忘记把application对象的visiable属性设置为True,否则你是看不到打开的文档的。
OK,可以编译运行啦。如下图:
3.查看Word文档信息
下面,我们来看一下文档的有关信息,对应上图中的文档信息按钮(监听器OnShowInfo):
[csharp]view plaincopy- privatevoidOnShowInfo(objectsender,EventArgse)
- {
- System.Diagnostics.Debug.WriteLine("当前打开文档数量:"+m_word.Documents.Count.ToString()+"/n");
- System.Diagnostics.Debug.WriteLine(m_word.ActiveDocument.Paragraphs.Count.ToString());
- }
由于Word里面的对象属性实在是太多,这里也就随便选择两个吧。第一行是当前打开文档数量,第二行是当前激活的文档的自然段个数:
可以看到分别输出1和2,没错吧。
4.关闭Word文档,退出Word应用程序
最后,我们再来看看如何关闭吧,同样的,先添加按钮,然后添加OnClose()监听器。
[csharp]view plaincopy