码迷,mamicode.com
首页 > Windows程序 > 详细

c# word interop encrypt with password protect with password

时间:2019-01-01 18:52:30      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:system   int   wds   ica   change   man   dap   orm   tostring   

		public static void EncryptWithPassword(string unEncryptedWordPath, string password)
		{
			Word.Application wordApp = null;
			Word.Document document = null;

			try
			{
				object missung = System.Type.Missing;
				object odocPath = unEncryptedWordPath;
				object opassword = password;
			
				wordApp = new Word.Application();
				document = wordApp.Documents.Open(odocPath);
				document.Password = password;
				document.SaveAs(ref odocPath, Word.WdSaveFormat.wdFormatDocumentDefault);
			}
			catch (Exception e)
			{
				string s = e.ToString();
				Console.WriteLine(s);
				throw;
			}
			finally
			{
				Quit(wordApp, document);
			}
		}




        public static void Quit(Word._Application wordApp, Word._Document doc)
        {
			if(doc != null)
				((Word._Document)doc).Close(Word.WdSaveOptions.wdDoNotSaveChanges);
			object ofalse = false;
			//ref false   to prevent the Word Process Hang in Task Manager
			if(wordApp !=null && wordApp.Application != null)
				((Word._Application)wordApp.Application).Quit(ref ofalse, ref ofalse, ref ofalse);
        }

  

c# word interop encrypt with password protect with password

标签:system   int   wds   ica   change   man   dap   orm   tostring   

原文地址:https://www.cnblogs.com/wwwblender-3dcn/p/10205469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!