【发布时间】: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();
}
【问题讨论】: