2016-02-25 28 views
8

W programie Microsoft Word utworzyłem plik openxml.doc (*. Docx) podany poświadczenia "abc" jako Readpassword i "xyz" jako WritePassword .Po wyświetleniu błędu podczas zabezpieczania hasłem dokument programu Word OpenXL zostanie ponownie zapisany jako chroniony hasłem program Word w pakiecie Office 2010

teraz muszę konwertować openxml.doc do binary.doc (WdSaveFormat = 0) dokument tworzony jest z powodzeniem jako Binary.doc za pomocą poniższego kodu

// Convert OpenXml.doc into binary.doc  
Convert(@"C:\Test\OpenXml.doc", @"C:\Test\binary.doc", WdSaveFormat.wdFormatDocument); 

// Convert a Word .docx to Word 2003 .doc 
public static void Convert(string input, string output, WdSaveFormat format) 
{ 
    // Create an instance of Word.exe 
    Word._Application oWord = new Word.Application(); 

    // Make this instance of word invisible (Can still see it in the taskmgr). 
    oWord.Visible = false; 

    // Interop requires objects. 
    object oMissing = System.Reflection.Missing.Value; 
    object isVisible = true; 
    object readOnly = false; 
    object oInput = input; 
    object oOutput = output; 
    object oFormat = format; 
    object oNewPassword = "xyz"; 
    object oOldPassword = "abc"; 
    object test = null; 

    try 
    { 
     // Load a document into our instance of word.exe 
     // suppose password "abc" 
     Word._Document oDoc = oWord.Documents.Open(ref oInput, ref oMissing, 
           ref readOnly, ref oMissing, oOldPassword, 
           ref oMissing, ref oMissing, oNewPassword, 
           ref oMissing, ref oMissing, ref oMissing, 
           ref isVisible, ref oMissing, ref oMissing, 
           ref oMissing, ref oMissing); 

     // Make this document the active document. 
     oDoc.Activate(); 

     // Save this document in Word 2003 format. 
     oDoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, 
        ref oOldPassword, ref oMissing, 
        oNewPassword, ref oMissing, ref oMissing, 
        ref oMissing, ref oMissing, ref oMissing, 
        ref oMissing, ref oMissing, ref oMissing, 
        ref oMissing, ref oMissing); 
     Console.WriteLine(test); 
     // Always close Word.exe. 
     oWord.Quit(ref oMissing, ref oMissing, ref oMissing); 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
} 

ale gdy próbuje otworzyć dokument ręcznie lub z kodu akceptuje Readpassword ('abc') jak pokazano poniżej

enter image description here

ale kiedy próbuje dać WritePassword ('xyz') robi to zaakceptować i pokazano hasło nieprawidłowe error.Please czek poniżej screeny

enter image description here

enter image description here

Odpowiedz

4

Przy kodzie pod warunkiem, że nie jestem w stanie poprawnie ustawić odczytu/zapisu haseł. Wygląda na to, że program Word nie może zmienić formatu zapisu i zachować jednocześnie hasła do odczytu/zapisu (może to być błąd lub prosty nieobsługiwany scenariusz).

Jednak jest to bardzo proste obejście: Wystarczy zapisać dokument chwilowo bez hasła, a następnie ponownie ustawić hasło:

public static void Convert(string input, string output, Word.WdSaveFormat format) 
{ 
    // Create an instance of Word.exe> 
    var oWord = new Word.Application(); 

    // open the protected document 
    var oDoc = oWord.Documents.Open(input, PasswordDocument: "abc", WritePasswordDocument: "xyz"); 

    // save the document without password first 
    oDoc.SaveAs(FileName: output, Password: "", WritePassword: ""); 

    // close and reopen 
    oDoc.Close(); 
    oDoc = oWord.Documents.Open(output); 

    // set the password 
    oDoc.SaveAs(FileName: output, FileFormat: format, Password: "abc", WritePassword: "xyz"); 

    oWord.Quit(); 
} 
+0

Więc to jest błąd! Ładne obejście! –

Powiązane problemy