2016-02-25 77 views
8

In microsoft word ho creato il file openxml.doc (*. Docx) con le credenziali 'abc' come Readpassword e 'xyz' come WritePassword .Visualizzazione dell'errore quando protetto da password Il documento Word di OpenXml viene resaved come parola binario protetta da password in ufficio 2010

Ora devo convertire openxml.doc a binary.doc (wdSaveFormat = 0) il documento viene creato con successo come Binary.doc utilizzando sottostante Codice

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

Ma quando si tenta di aprire il documento manualmente o dal codice accetta readpassword ('abc'), come illustrato di seguito

enter image description here

ma quando cerca di dare WritePassword ('xyz') doesnt accettare e mostrato la password di controllo errore nella trasmissione errata di seguito le immagini

enter image description here

enter image description here

risposta

4

Con il codice che hai fornito non sono anche in grado di impostare correttamente il lettura/scrittura delle password. Sembra che Word non sia in grado di modificare il formato di salvataggio e mantenere le password di lettura/salvataggio allo stesso tempo (potrebbe trattarsi di un bug o di uno scenario non supportato).

Tuttavia, v'è una soluzione molto semplice: basta salvare il documento temporaneo, senza password e quindi impostare nuovamente la password:

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

quindi è un bug! Bel trucco! –