【问题标题】:save the new document result from Word mailmerge in C#在 C# 中保存来自 Word 邮件合并的新文档结果
【发布时间】:2020-01-17 15:05:44
【问题描述】:

我正在使用 Word for Office 365 将邮件合并到新文档

我正在尝试将新文档保存到指定位置。但是我不知道如何获取对 MailMerge.Execute 之后创建的这个新文档的引用。

我目前正在使用 ActiveDocument,但是除了在 ActiveDocument 出现之前关闭的原始文档之外,我还必须关闭另一个文档(我猜是有错误的文档)我要保存。

是否有更可靠的方法来获取对 MailMerge.Execute 创建的文档的引用?

  static void Main(string[] args)
    {
        string inDoc = @"C:\bob\doc.docx";
        string data = @"C:\bob\data.csv";
        string outDoc = @"C:\bob\out.docx";

        Application wordApp = new Application();
        Document wordDoc = wordApp.Documents.Open(inDoc);

        wordDoc.MailMerge.OpenDataSource(data);

        wordDoc.MailMerge.Destination = WdMailMergeDestination.wdSendToNewDocument;
        wordDoc.MailMerge.SuppressBlankLines = true;
        wordDoc.MailMerge.Execute(false);

        // Close the input document
        wordDoc.Close(false);

        // Active document is now what?  The error document?
        // Close this too
        wordApp.ActiveDocument.Saved = true;
        wordApp.ActiveDocument.Close(false);

        // Now have reference to the new document
        wordApp.ActiveDocument.SaveAs2(outDoc, WdSaveFormat.wdFormatDocumentDefault);
        wordApp.ActiveDocument.Close(false);

        wordApp.Quit();
    }

【问题讨论】:

    标签: c# ms-word mailmerge


    【解决方案1】:

    是否有更可靠的方法来获取对文档的引用 由 MailMerge.Execute 创建?

    很遗憾,没有。如果Execute 方法会返回如果执行是针对新文档而不是打印机或电子邮件,那将会很有用...

    我要做的是在执行邮件合并之前创建一个数组/列表/集合/字典/所有当前打开的文档。然后检查ActiveDocument 是否在那个“列表”中。如果没有,那你就选对了。如果是,则循环wordApp.Documents 集合,直到找到不在“列表”中的集合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 2014-02-28
      • 2019-08-29
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多