VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C# WORD INTEROP ENCRYPT WITH PASSWORD PROTECT WITH

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);
}