C# windows 表单应用程序 Word Interop 适用于 Windows 7 但不适用于 Windows 10

C# windows forms application Word Interop works on Windows 7 but not Windows 10

我编写了一个应用程序来帮助用户使用 C# 生成字母。我创建了以富文本格式保存在 SQL 服务器数据库中的模板。我的开发机器是 Windows 7,我使用 Visual Studio 2019 对应用程序进行编码。我使用 NuGet 添加了 Word 的互操作引用。该应用程序配置为 Release,平台 Active(Any CPU),目标为 x86。它是一个 ClickOnce 应用程序,从单独目录中的共享驱动器安装,但与保存字母的驱动器相同。

应用程序在我的机器上运行正常,但在 Windows 10 用户的机器上运行不正常。当它尝试保存文件时,她收到一条错误消息,提示“抱歉,我们找不到该文件”。我们都有 Word 2016。两台机器都是 64 位的。我将这封信保存为 Word 中的备份,然后导出为 PDF。在导出为 PDF 之前,代码无法保存 Word 文档。请参阅下面的代码片段:

    public static void SaveToWord2(string CoverLetter, string LetterText, string FileSave, 
                                   string BackUpSave, ref string ErrorString)
    {
        try
        {

            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
            string HeaderFooterFile = Properties.FileResources.HeaderFooterTemplate;

            Document oDoc = new Document();
            oDoc = oWord.Documents.Add(@HeaderFooterFile);
            oDoc.Activate();
            try
            {
                Clipboard.Clear();
                Clipboard.SetText(CoverLetter, TextDataFormat.Rtf);
                oDoc.ActiveWindow.Selection.Paste();
                Clipboard.Clear();
                Clipboard.SetText(LetterText, TextDataFormat.Rtf);
                oDoc.ActiveWindow.Selection.Paste();
                //01/26/2021 JS having trouble with the save on Pam's machine so going to try to capture the correct error.
                try
                {
                    oDoc.SaveAs(@BackUpSave);
                }
                catch (Exception exBU)
                {
                    ErrorString = "Error trying to save " + @BackUpSave + ": " + exBU.Message;
                }
                try
                {
                    oDoc.ExportAsFixedFormat(@FileSave, WdExportFormat.wdExportFormatPDF);
                }
                catch (Exception exPDF)
                {
                    if (string.IsNullOrEmpty(ErrorString))
                    {
                        ErrorString = "Error trying to save " + @FileSave + " PDF: " + exPDF.Message;
                    }
                    else
                    {
                        ErrorString += " and Error trying to save " + @FileSave + " PDF: " + exPDF.Message;
                    }
                }
            }
            catch (Exception exInner)
            {
                ErrorString = exInner.Message;
                MessageBox.Show(exInner.Message, Properties.LetterResources.SaveToWord, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            Clipboard.Clear();
            oWord.Quit(SaveChanges: 0);
            Marshal.FinalReleaseComObject(oWord);
            //oWord.Visible = true;
        }
        catch (Exception ex)
        {
            ErrorString = ex.Message;
            MessageBox.Show(ex.Message, Properties.LetterResources.SaveToWord, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

错误发生在 oDoc.SaveAs 行,但仅当来自 Windows 10 计算机的 运行 时才发生。最初,我使用我机器上的互操作,但后来将其更改为 NuGet 互操作,但这并没有解决问题。我尝试将 Embed Interop Types 更改为 False 但这并没有解决任何问题,所以我将其改回。互操作引用的别名 属性 是全局的,特定版本 属性 是 True。由于富文本,我担心更改文档编写器的类型。对于 Windows 10 用户,应用程序的其余部分工作正常。有什么想法吗?

OpenXML 无法正常工作,因为它不允许我正确添加 headers 或导出为 PDF。尽管我非常想使用 OpenXML,但我最终还是回到了 Word Interop。我能够通过如下设置参考属性来解决问题:

嵌入互操作类型 = False 复制本地 = True

现在无论哪个操作系统运行该程序都可以保存。我有上面的部分答案,但忽略了 Copy Local = true 部分。