首页 > 代码库 > word的锁定与解锁

word的锁定与解锁

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Net.Mime; 6 using System.ComponentModel; 7 using System.IO; 8 using Microsoft.Office.Interop.Word; 9 using MSWord = Microsoft.Office.Interop.Word;10 using Microsoft.Office.Tools.Word;11 //using Word = Microsoft.Office.Interop.Word;12 13 14 namespace ConsoleApplication415 {16     class Program17     {18         static void Main(string[] args)19         {20             string fileName = "D:\\测试.doc";21             string passWord = "112233";22             Protect(passWord, fileName);23             Unprotected(passWord, fileName);24         }25 26         public static void Protect(string passWord,string FileName)27         {28             MSWord.Application WordApp;29             MSWord.Document WordDoc;30             try{31                 object missing = Type.Missing;32                 WordApp = new MSWord.ApplicationClass();33                 object fileName = FileName ;34                 object readOnly = false;35                 object isVisible = true;36                 object objFalse = false;37                 WordApp.CommandBars["Control Toolbox"].Enabled = false;38                 //打开文档39                 WordDoc = WordApp.Documents.Open(ref fileName ,40                     ref missing,ref readOnly ,ref missing ,ref missing ,ref missing,41                     ref missing ,ref missing ,ref missing ,ref missing ,ref missing,42                     ref missing ,ref missing , ref isVisible ,ref missing ,ref missing );43                 object objProtectPWD = passWord ;44                 object NoSet = null;45                 WordDoc .Protect2002(WdProtectionType.wdAllowOnlyFormFields,ref NoSet,ref objProtectPWD);46                 WordDoc .Save();47                 WordDoc .Close(ref missing,ref missing,ref missing);48                 WordApp.Quit(ref missing, ref missing, ref missing);49             }50             catch(Exception ex)51             {52             }53         }54 55         public static void Unprotected(string passWord, string FileName)56         {57             MSWord.Application WordApp;58             MSWord.Document WordDoc;59             WordApp = new MSWord.ApplicationClass();60             WordApp.CommandBars["Control Toolbox"].Enabled = true ;61             object PassWord = passWord;62             object fileName = FileName;63             object missing = Type.Missing;64             object readOnly = false;65             object isVisible = true;66             object objFalse = false;67             WordDoc = WordApp.Documents.Open(ref fileName,68                     ref missing, ref readOnly, ref missing, ref missing, ref missing,69                     ref missing, ref missing, ref missing, ref missing, ref missing,70                     ref missing, ref missing, ref isVisible, ref missing, ref missing);71             WordDoc.Activate();72             //检查是否有密码73             if (WordDoc.HasPassword)74             {75                 WordDoc.Password = null;76             }77             //检查是否为Word文档设置保护功能,没有设置保护功能则直接解除密码保护78             if (WordDoc.ProtectionType != WdProtectionType.wdNoProtection)79             {80                 WordDoc.Unprotect(ref PassWord);81             }82 83             WordDoc.Save();84             WordDoc.Close(ref missing, ref missing, ref missing);85             WordApp.Quit(ref missing, ref missing, ref missing);86          }87        88     }89 }