在 C# 中保存来自 Word mailmerge 的新文档结果
save the new document result from Word mailmerge in C#
我正在使用 Word for Office 365 对新文档执行邮件合并
我正在尝试将新文档保存到指定位置。但是我不知道如何获得对 MailMerge.Execute.
之后创建的这个新文档的引用
我目前正在使用 ActiveDocument,但是除了我在 ActiveDocument 之前关闭的原始文档之外,还有另一个文档 'in the way'(我猜它是有错误的文档)我必须关闭是我想保存的东西。
是否有更可靠的方法来获取对 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();
}
Is there a more robust way of getting a reference to the document
created by MailMerge.Execute?
很遗憾,没有。如果 Execute
方法会 return 如果执行是针对新文档而不是打印机或电子邮件...
我要做的是在执行邮件合并之前为所有当前打开的文档创建一个 array/list/collection/dictionary/whatever。然后检查ActiveDocument
是否在"list"中。如果没有,那么您选对了。如果是,则循环 wordApp.Documents
集合,直到找到一个不在 "list" 中的集合。
我正在使用 Word for Office 365 对新文档执行邮件合并
我正在尝试将新文档保存到指定位置。但是我不知道如何获得对 MailMerge.Execute.
之后创建的这个新文档的引用我目前正在使用 ActiveDocument,但是除了我在 ActiveDocument 之前关闭的原始文档之外,还有另一个文档 'in the way'(我猜它是有错误的文档)我必须关闭是我想保存的东西。
是否有更可靠的方法来获取对 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();
}
Is there a more robust way of getting a reference to the document created by MailMerge.Execute?
很遗憾,没有。如果 Execute
方法会 return 如果执行是针对新文档而不是打印机或电子邮件...
我要做的是在执行邮件合并之前为所有当前打开的文档创建一个 array/list/collection/dictionary/whatever。然后检查ActiveDocument
是否在"list"中。如果没有,那么您选对了。如果是,则循环 wordApp.Documents
集合,直到找到一个不在 "list" 中的集合。