2016-02-25 3 views
8

in Microsoft Word in Office 2010 erhält erneut gespeichert Ich habe openxml.doc erstellt (*. Docx-Datei) angegebenen Zugangsdaten ‚abc‘ als Readpassword und ‚xyz‘ als WritePassword .Anzeigen Fehler, wenn ein Passwort geschützt OpenXml Word-Dokument als Passwort Binärwort geschützt

Jetzt muss ich openxml.doc zu binary.doc konvertieren (WdSaveFormat = 0) wird das Dokument erfolgreich als Binary.doc erstellt unter Code

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

Aber wenn ich versuche Dokument manuell oder öffnen von Code es akzeptiert Readpassword ('abc'), wie unten

enter image description here

gezeigt, aber wenn versucht WritePassword ('xyz') es tut akzeptieren und gezeigt Passwort falsch error.Please Prüfung unter Screenshots zu geben

enter image description here

enter image description here

Antwort

4

Mit dem Code versehen Sie, ich bin auch Passwörter nicht in der Lage, um die Lese richtig eingestellt/schreiben. Es scheint, dass Word nicht in der Lage ist, das Speicherformat zu ändern und die Kennwörter zum Lesen/Speichern gleichzeitig beizubehalten (dies könnte ein Fehler oder einfach ein nicht unterstütztes Szenario sein).

Allerdings gibt es eine sehr einfache Abhilfe: Einfach das Dokument vorübergehend ohne Kennwort speichern und dann das Passwort erneut ein:

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

So ist es ein Fehler! Schöne Problemumgehung! –

Verwandte Themen