C#操作Word文档(加密、解密、对应书签插入分页符)

8/3/2015来源:C#应用人气:1810

C#操作Word文档(加密、解密、对应书签插入分页符)

最近做一个项目,客户要求对已经生成好的RTF文件中的内容进行分页显示,由于之前对这方面没有什么了解,后来在网上也找了相关的资料,并结合自己在MSDN上面的查找,后来总算把问题给解决掉啦。下面对C#操作Word文档(加密、解密、插入分页符)做一个简单的总结,希望对一些朋友有所帮忙吧。^_^

写代码之前,需要引用对应的DLL文件:

1、Interop.Microsoft.Office.Interop.Word.dll (网上可以下载)

2、mscorlib.dll (添加引用--->.NET中即可找到)

 1 using Microsoft.Office.Interop.Word; 2 using MSWord = Microsoft.Office.Interop.Word; 3 using System.Reflection; 4  5 PRivate void button1_Click(object sender, System.EventArgs e) 6  { 7             //Word文档保护密码 8             string Pass = "ITIS@997168"; 9             object PassWord = Pass;10             MSWord.application wordApp;  //Word应用程序变量11             MSWord.Document wordDoc;    //Word文档变量12             try13             {14                 object Nothing = Missing.Value;  //初始化15                 wordApp = new MSWord.ApplicationClass();16 17                 // 打开已存在的Word18                 object FileName = @"E:\archive\CMPLatest_2117_230614-1053.Rtf";19                 object readOnly = false;20                 object isVisible = true;21                 object objFalse = false;22 23                 wordDoc = wordApp.Documents.Open(ref FileName, ref Nothing, ref readOnly, ref Nothing, ref PassWord, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);24 25                 //激活Word文档26                 wordDoc.Activate();27                 //判断是否有密码28                 if (wordDoc.Haspassword)29                 {30                     wordDoc.Password = null;31                 }32 33                 //检查是否为Word文档设置保护功能,没有设置保护功能,就解除密码保护34                 if (wordDoc.ProtectionType != WdProtectionType.wdNoProtection)35                 {36                     wordDoc.Unprotect(ref PassWord);37                 }38 39                 //跳转到指定书签40                 object toMark = MSWord.WdGoToItem.wdGoToBookmark;41                 //分页符42                 object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;  43 44                 //定义书签名称  PartB45                 object BookMarkName_b = "bmf_b";46                 wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_b);47                 //插入分页符48                 wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);49                 50                 //定义书签名称  PartC151                 object BookMarkName_c1 = "bmf_c1";52                 wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_c1);53                 //插入分页符54                 wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);55 56                 //定义书签名称  PartC257                 object BookMarkName_c2 = "bmf_c2";58                 wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_c2);59                 //插入分页符60                 wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);61 62                 //对Word文档进行加密保护63                 if(PassWord.ToString() != null)64                 {65                     wordDoc.Protect(WdProtectionType.wdAllowOnlyReading, ref objFalse, ref PassWord, ref Nothing, ref Nothing);66                 }67                 68 69                 //将插入分页符后的Word文档保存一下70                 wordDoc.SaveAs(ref FileName, ref Nothing, ref Nothing, ref Nothing, ref objFalse, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);71 72                 //标记为最终状态,禁止弹出对话框73                 //wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;74                 //标记为最终状态75                 //wordDoc.Final = true;76 77                 //关闭Word文档78                 wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);79                 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);80             }81             catch(Exception ex)82             {83                 84             }85 }