2
votes

Password protected OpenXml Word document(ie.document created in wdFormatDocument=12) that gets resaved as a password protected binary Word document(wdFormatDocument=0) gives error "The password is incorrect.Word cannot open the document."

In below code "74.doc" is created using wdFormatDocument=12 when converting this doc back to wdFormatDocument=0 it is giving error. Debug the code and search on net but could not find out the exact root cause why this happens.

The error is triggered by this line:

oDoc = oWord.Documents.Open(ref oInput, ref oMissing, ref readOnly, ref oMissing, oReadPassword, ref oMissing, ref oMissing, oWritePassword , ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

This is the code

    class Docx
    {
        public static void Start()
        {
            // Convert Input.docx into Output.doc
            Convert(@"C:\Test\74.doc", @"C:\Test\74_0.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 oWritePassword = "abc";
            object oReadPassword = "xyz";

            try
            {
                // Load a document into our instance of word.exe
                Word._Document oDoc = oWord.Documents.Open(ref oInput, ref oMissing, ref readOnly, ref oMissing, oReadPassword, ref oMissing, ref oMissing, oWritePassword, 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, oReadPassword, ref oMissing, oWritePassword, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                // Always close Word.exe.
                oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

Update: when readonly as true is specified in the arguments, the document is opening sucessfully. But setting to false is causing the error.

1
Please also provide the code that triggers the error, indicating which line is causing it.Cindy Meister
@Cindy: on this lin eits getting erroWord._Document oDoc = oWord.Documents.Open(ref oInput, ref oMissing, ref readOnly, ref oMissing, oReadPassword, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);John
@Cindy when pass readonly as true the document is opening sucessfully.But set to false its showing errorJohn

1 Answers

0
votes

Take a careful look at the objects you're passing to the Document.SaveAs method, specifically

        object oWritePassword = "abc";
        object oReadPassword = "xyz";

You're assigning both Read and Write passwords. If you do that, then you need to take it into consideration when opening the document.

Read-only password prevents the document from opening unless the password is supplied. In Documents.Open - this is the fifth argument.

Write-only password prevents the file from being saved to the same name. It does not, however, prevent the user editing the document. This is the eight argument in Documents.Open.

The third argument, ReadOnly, applies the same "protection" as the Write-only password - the user can edit the document but not save the changes back to the original document.

In this situation, where you've used both types of passwords, the third argument is irreleveant. You should assign oMissing to it, nothing else. If the user should be able to edit and save the changes back to the document, also supply the write password when opening the document.