本文正式开始在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]class="Apple-converted-space"> view plaincopy
- using MSWord    =   Microsoft.Office.Interop.Word;  
 
 
2.打开Word文档
然后给Form添加一个Load事件的消息响应函数OnLoad:
 
 
 
 
好了。下一步,我们完善OnLoad函数:
 
[csharp] view plaincopy
- private MSWord.Application m_word;  
- private MSWord.Document m_doc;  
-   
-         public Form1()  
-         {  
-             InitializeComponent();  
-         }  
-           
-         private void OnLoad(object sender, EventArgs e)  
-         {  
-             m_word = new MSWord.Application();  
-         }   
在OnLoad中我们实例化了一个Word的Application对象,代表Word2007应用程序。
 
 
这样只打开了一个应用程序的空壳,里面还没有文档。下面我们就打开一个已有的文档吧。
在打开之前,我们先添加一个按钮,然后为其设置Click事件的监听器OnOpen()
 
[csharp] view plaincopy
- private void OnOpen(object sender, EventArgs e)  
-         {  
-   
-             Object filename = "test.docx";  
-             Object filefullname = @"C:\Users\David_ss\Desktop\项目管理\test.docx";  
-             Object confirmConversions = Type.Missing;  
-             Object readOnly = Type.Missing;  
-             Object addToRecentFiles = Type.Missing;  
-             Object passwordDocument = Type.Missing;  
-             Object passwordTemplate = Type.Missing;  
-             Object revert = Type.Missing;  
-             Object writePasswordDocument = Type.Missing;  
-             Object writePasswordTemplate = Type.Missing;  
-             Object format = Type.Missing;  
-             Object encoding = Type.Missing;  
-             Object visible = Type.Missing;  
-             Object openConflictDocument = Type.Missing;  
-             Object openAndRepair = Type.Missing;  
-             Object documentDirection = Type.Missing;  
-             Object noEncodingDialog = Type.Missing;  
-   
-             for (int i = 1; i <= m_word.Documents.Count; i++)  
-             {  
-                 String str = m_word.Documents[i].FullName.ToString();  
-                 if (str == filefullname.ToString())  
-                 {  
-                     MessageBox.Show("请勿重复打开该文档");  
-                     return;  
-                 }  
-             }  
-             try  
-             {  
-                 m_word.Documents.Open(ref filefullname,  
-                         ref confirmConversions, ref readOnly, ref addToRecentFiles,  
-                         ref passwordDocument, ref passwordTemplate, ref revert,  
-                         ref writePasswordDocument, ref writePasswordTemplate,  
-                         ref format, ref encoding, ref visible, ref openConflictDocument,  
-                         ref openAndRepair, ref documentDirection, ref noEncodingDialog  
-                         );  
-                 m_word.Visible = true;  
-   
-                 
-                 
-             }  
-             catch (System.Exception ex)  
-             {  
-                 MessageBox.Show("打开Word文档出错");  
-             }  
-         }   
 
可以看到,这里调用的是Documents对象的Open方法,参数很多,感兴趣的朋友可以参考MSDN。
 
上面代码中我直接写出了文件路径,当然更好的方法是弹出对话框然后选择文件。
代码中也检查了该文档是否已经打开,这样也就避免了重复打开同一文档两次。另外需要注意的是,Word应用程序打开文档的数量是从1开始数的(即1 based),不是常见的从0开始,这点需要注意一下:
for(int i=1;i<m_word.Documents.Count;i++)
 
在Open方法调用完成后,别忘记把application对象的visiable属性设置为True,否则你是看不到打开的文档的。
 
OK,可以编译运行啦。如下图:
 
 
 
3.查看Word文档信息 
下面,我们来看一下文档的有关信息,对应上图中的文档信息按钮(监听器OnShowInfo):
 
[csharp] view plaincopy
- private void OnShowInfo(object sender, EventArgs e)  
-       {  
-           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
- private void OnClose(object sender, EventArgs e)  
-       {  
-           
-           m_word.NormalTemplate.Saved = true;  
-   
-           
-           Object saveChanges = MSWord.WdSaveOptions.wdSaveChanges;  
-           Object originalFormat = Type.Missing;  
-           Object routeDocument = Type.Missing;  
-           m_word.Documents.Close(ref saveChanges,ref originalFormat, ref routeDocument);  
-             
-           
-           if (m_word.Documents.Count == 0)  
-           {  
-               m_word.Quit(Type.Missing, Type.Missing, Type.Missing);  
-           }  
-             
-       }  
这里面需要注意的是为了防止弹出 normal.dotm被使用的对话框,最好先自动保存模板。
 
然后设置好你需要的saveOption.有三种,分别是
 
- wdSaveChanges
- wdDoNotSaveChanges
- wdPromptToSaveChanges
OK,今天就介绍这么多吧。下次再介绍有关Selection对象和Range对象吧。
 
-------------------------------华丽分割--------------------------------------
 
备注:MSDN------>Word.Application对象 
MSDN------>Word.Document对象