受密码保护的OpenXml Word文档重新保存为受密码保护的二进制Word

本文关键字:Word 密码保护 保存 二进制 OpenXml 文档 新保存 | 更新日期: 2023-09-27 18:35:05

受密码保护的OpenXml Word文档(即在wdFormatDocument

=12中创建的文档)被重新保存为受密码保护的二进制Word文档(wdFormatDocument=0)给出错误"密码不正确。Word 无法打开文档。

在下面的代码"74.doc"是使用 wdFormatDocument=12 创建的,当将此文档转换回 wdFormatDocument=0 时,它给出了错误。调试代码并在网上搜索,但无法找出发生这种情况的确切根本原因。

错误由以下行触发:

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

这是代码

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

更新:当参数中将只读指定为 true 时,文档将成功打开。但是设置为 false 会导致错误。

受密码保护的OpenXml Word文档重新保存为受密码保护的二进制Word

仔细查看要传递给 Document.SaveAs 方法的对象,特别是

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

您同时分配读取和写入密码。如果这样做,则需要在打开文档时将其考虑在内。

只读密码可防止文档打开,除非提供密码。在 Documents.Open 中 - 这是第五个参数。

只写密码可防止将文件保存为同名。但是,它不会阻止用户编辑文档。这是 Documents.Open 中的八个参数。

第三个参数 ReadOnly 应用与只写密码相同的"保护"——用户可以编辑文档,但不能将更改保存回原始文档。

在这种情况下,如果您使用了两种类型的密码,则第三个参数无关紧要。您应该为其分配 oMissing ,而不是其他任何内容。如果用户应该能够编辑更改并将其保存回文档,请在打开文档时提供写入密码。