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.