【发布时间】:2016-06-07 23:26:03
【问题描述】:
在 microsoft word 中,我创建了 openxml.doc(*.docx) 文件,将凭据“abc”作为 Readpassword,将“xyz”作为 WritePassword。
现在我必须将 openxml.doc 转换为 binary.doc(WdSaveFormat=0) 使用以下代码成功地将文档创建为 Binary.doc
// 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;
}
}
但是当尝试手动或从代码打开文档时,它接受 Readpassword('abc'),如下所示
但是当尝试给 WritePassword('xyz') 时它不接受并显示密码不正确的错误。请查看下面的屏幕截图
【问题讨论】:
标签: c# .net ms-word com office-interop