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

C# 受密码保护的OpenXml Word文档重新保存为受密码保护的二进制Word,c#,.net,ms-word,office-interop,com-interop,C#,.net,Ms Word,Office Interop,Com Interop,受密码保护的OpenXml Word文档(即在wdFormatDocument=12中创建的文档)重新保存为受密码保护的二进制Word文档(wdFormatDocument=0),该文档出现错误“密码不正确。Word无法打开该文档。” 在下面的代码中,“74.doc”是使用wdFormatDocument=12创建的,当将此文档转换回wdFormatDocument=0时,会出现错误。调试代码并在网上搜索,但无法找到发生这种情况的确切根本原因 该错误由以下行触发: oDoc = oWord.Do

受密码保护的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;
            }
        }
    }

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

请仔细查看传递给Document.SaveAs方法的对象,尤其是

        object oWritePassword = "abc";
        object oReadPassword = "xyz";
您正在分配读密码和写密码。如果这样做,那么在打开文档时需要考虑到这一点

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

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

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


在这种情况下,您使用了两种类型的密码,第三个参数是不相关的。你应该给它赋值,不要给它赋值。如果用户能够编辑并将更改保存回文档,请在打开文档时提供写入密码。

请同时提供触发错误的代码,指明是哪一行导致了错误。@Cindy:在这条lin上,它是一个错误。\u document oDoc=oWord.Documents.Open(引用输入、引用省略、引用只读、引用省略、或ADPassword、引用省略、引用省略、引用省略、引用省略、引用省略、引用省略、引用可见、引用省略、引用省略、引用省略、引用省略);@Cindy当将readonly传递为true时,文档成功打开。但设置为false时,其显示错误错误原因是上面代码中的writepassword参数第8个参数,当我们将writepassword参数传递为null时,文档在只读模式下成功打开。但我仍在查找文档无法打开的根本原因提供writepassword。我不确定这会给我们带来什么影响。我做了一些实验,使用Word将openxml文档重新保存为二进制文件,最初我能够复制该行为。当我尝试使用新密码重新保存时,我可以重新打开该文档。尝试了几个变体后,我再也无法将Word保存为二进制文件我怀疑Word做了一些奇怪的事情,可能是缓存了一些改变了其行为的东西。请查看链接以了解有关此问题的更多详细信息