1. 程式人生 > >C# 設定word不可編輯

C# 設定word不可編輯

 Word.Application word = new Word.Application();
            Object missing = Type.Missing;
            Object Password = "11111";           
            Object filename = @"C:\Users\ThinkPad\Desktop\aaa.doc";

            Microsoft.Office.Interop.Word.Document wordDoc = word.Documents.Open(ref filename, ref missing, ref  

                                                               missing, ref missing, ref missing, ref missing, ref  

                                                               missing, ref missing, ref missing, ref missing, ref 

                                                               missing, ref missing, ref missing, ref missing, ref 

                                                               missing, ref missing);
            

              if (word.ActiveDocument.ProtectionType == WdProtectionType.wdAllowOnlyComments)
              {
                  word.ActiveDocument.Unprotect(ref Password);
              }

             object objFalse = false;

             if (word.ActiveDocument.ProtectionType == WdProtectionType.wdNoProtection)
             {
                 word.ActiveDocument.Protect(WdProtectionType.wdAllowOnlyComments, ref objFalse, ref Password, ref missing, ref missing);
             }

             wordDoc.Save();
             wordDoc.Close(ref missing, ref missing, ref missing);
             word.Quit(ref missing, ref missing, ref missing);