首页 > 学院 > 开发设计 > 正文

C#操作Word (2)-- 打开&关闭Word文档

2019-11-17 02:57:06
字体:
来源:转载
供稿:网友
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
  1. usingMSWord=Microsoft.Office.Interop.Word;

2.打开Word文档

然后给Form添加一个Load事件的消息响应函数OnLoad:

好了。下一步,我们完善OnLoad函数:

[csharp]view plaincopy
  1. PRivateMSWord.applicationm_word;
  2. privateMSWord.Documentm_doc;
  3. publicForm1()
  4. {
  5. InitializeComponent();
  6. }
  7. privatevoidOnLoad(objectsender,EventArgse)
  8. {
  9. m_word=newMSWord.Application();
  10. }

在OnLoad中我们实例化了一个Word的Application对象,代表Word2007应用程序。

这样只打开了一个应用程序的空壳,里面还没有文档。下面我们就打开一个已有的文档吧。

在打开之前,我们先添加一个按钮,然后为其设置Click事件的监听器OnOpen()

[csharp]view plaincopy
  1. privatevoidOnOpen(objectsender,EventArgse)
  2. {
  3. Objectfilename="test.docx";
  4. Objectfilefullname=@"C:/Users/David_ss/Desktop/项目管理/test.docx";
  5. ObjectconfirmConversions=Type.Missing;
  6. ObjectreadOnly=Type.Missing;
  7. ObjectaddToRecentFiles=Type.Missing;
  8. ObjectpasswordDocument=Type.Missing;
  9. ObjectpasswordTemplate=Type.Missing;
  10. Objectrevert=Type.Missing;
  11. ObjectwritePasswordDocument=Type.Missing;
  12. ObjectwritePasswordTemplate=Type.Missing;
  13. Objectformat=Type.Missing;
  14. Objectencoding=Type.Missing;
  15. Objectvisible=Type.Missing;
  16. ObjectopenConflictDocument=Type.Missing;
  17. ObjectopenAndRepair=Type.Missing;
  18. ObjectdocumentDirection=Type.Missing;
  19. ObjectnoEncodingDialog=Type.Missing;
  20. for(inti=1;i<=m_word.Documents.Count;i++)
  21. {
  22. Stringstr=m_word.Documents[i].FullName.ToString();
  23. if(str==filefullname.ToString())
  24. {
  25. MessageBox.Show("请勿重复打开该文档");
  26. return;
  27. }
  28. }
  29. try
  30. {
  31. m_word.Documents.Open(reffilefullname,
  32. refconfirmConversions,refreadOnly,refaddToRecentFiles,
  33. refpasswordDocument,refpasswordTemplate,refrevert,
  34. refwritePasswordDocument,refwritePasswordTemplate,
  35. refformat,refencoding,refvisible,refopenConflictDocument,
  36. refopenAndRepair,refdocumentDirection,refnoEncodingDialog
  37. );
  38. m_word.Visible=true;
  39. //MessageBox.Show(m_word.Documents.Count.ToString());
  40. //MessageBox.Show(m_word.Documents[1].FullName.ToString());
  41. }
  42. catch(System.Exceptionex)
  43. {
  44. MessageBox.Show("打开Word文档出错");
  45. }
  46. }

可以看到,这里调用的是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
  1. privatevoidOnShowInfo(objectsender,EventArgse)
  2. {
  3. System.Diagnostics.Debug.WriteLine("当前打开文档数量:"+m_word.Documents.Count.ToString()+"/n");
  4. System.Diagnostics.Debug.WriteLine(m_word.ActiveDocument.Paragraphs.Count.ToString());
  5. }

由于Word里面的对象属性实在是太多,这里也就随便选择两个吧。第一行是当前打开文档数量,第二行是当前激活的文档的自然段个数:

可以看到分别输出1和2,没错吧。

4.关闭Word文档,退出Word应用程序

最后,我们再来看看如何关闭吧,同样的,先添加按钮,然后添加OnClose()监听器。

[csharp]view plaincopyFlashplayer"
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表