2016-02-25 21 views
8

microsoft word'de openxml.doc (*. Docx) dosyası oluşturuldu, readpassword olarak 'abc' ve Readpassword olarak 'xyz' yazıldı .Parola korumalı olduğunda hata gösteriliyor OpenXML Word belgesinde parola korumalı bir ikili Word olarak yeniden yazılabilir. Word 2010

Şimdi ben binary.doc için openxml.doc dönüştürmek zorunda (WdSaveFormat = 0) belge 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; 
    } 
} 

Ama aşağıda kullanarak Binary.doc olarak başarıyla oluşturulduktan deneyin elle veya kodundan belgeyi açmak için zaman

enter image description here

aşağıda gösterildiği gibi readpassword ('abc') kabul eder fakat WritePassword ('xyz') vermek çalıştığında ekran

aşağıda şifre yanlış hatası oldu.Lütfen çeki kabul ve gösterilen doesnt kod ile

enter image description here

cevap

4

enter image description here

ben de doğru şifreleri yazma/okuma ayarlamak mümkün değilim sağladı. Word'ün kayıt biçimini değiştiremediği ve okuma/kaydetme parolalarını aynı anda tutamayacağı anlaşılıyor (bu bir hata veya desteklenmeyen basit bir senaryo olabilir).

Ancak çok basit bir çözümü vardır: Sadece şifresiz geçici belgeyi kaydedin ve sonra parolayı yeniden ayarlayın:

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

Bu yüzden bir hata olduğunu! Güzel bir çözüm! –

İlgili konular