Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing error when password protected OpenXml Word document get resaved as a password protected binary Word in office 2010

In microsoft word i have created openxml.doc(*.docx) file given credentials 'abc' as Readpassword and 'xyz' as WritePassword.

Now i have to convert openxml.doc to binary.doc(WdSaveFormat=0) the document is created sucessfully as Binary.doc using below 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;
    }
}

But when try to open document manually or from code it accepts Readpassword('abc') as shown below

enter image description here

but when tries to give WritePassword('xyz') it doesnt accept and shown password incorrect error.Please check below screenshots

enter image description here

enter image description here

like image 606
John Avatar asked Feb 25 '16 09:02

John


People also ask

Can't open a password protected Word File?

Re: Can't Open Word document that is password protected A cumbersome workaround is to download the protected file, then open it in Word (or EXCEL). It will be protected, but a copy will now reside in the Download folder.

How do I convert a protected Word document?

Unlock a protected documentOn the Review tab, in the Protect group, click Restrict Editing. In the Restrict Formatting and Editing task pane, click Stop Protection. If you are prompted to provide a password, type the password.

How can I open a password protected docx File?

How to unlock DOCX files online. Click inside the file drop area to upload a DOCX file or drag & drop a DOCX file. Type password and click 'UNLOCK' button. Once your file is unlocked click on 'DOWNLOAD NOW' button.


1 Answers

With the code you provided I am also not able to correctly set the read/write passwords. It seems that Word is not able to change the save format and retain the read/save passwords at the same time (this might be a bug or simple an unsupported scenario).

However, there is a very simple workaround: Just save the document temporarily without password and then set the password again:

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();
}
like image 73
Dirk Vollmar Avatar answered Oct 05 '22 18:10

Dirk Vollmar