gpt4 book ai didi

c# - 在 office 2010 中将受密码保护的 OpenXml Word 文档重新保存为受密码保护的二进制 Word 时显示错误

转载 作者:太空狗 更新时间:2023-10-29 19:42:25 26 4
gpt4 key购买 nike

在 Microsoft Word 中,我创建了 openxml.doc(*.docx) 文件,凭据“abc”作为读取密码,“xyz”作为写入密码。

现在我必须将 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')

enter image description here

但是当尝试提供 WritePassword('xyz') 时,它不接受并显示密码不正确错误。请查看下面的屏幕截图

enter image description here

enter image description here

最佳答案

使用您提供的代码,我也无法正确设置读/写密码。 Word 似乎无法同时更改保存格式和保留读取/保存密码(这可能是一个错误或简单的不受支持的情况)。

但是,有一个非常简单的解决方法:暂时不设置密码保存文档,然后重新设置密码:

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();
}

关于c# - 在 office 2010 中将受密码保护的 OpenXml Word 文档重新保存为受密码保护的二进制 Word 时显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35623050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com