我使用.doc文件中的Interop.Word
创建了邮件合并。
它工作正常,从指定的数据源加载数据,我可以使用方法wDoc.MailMerge.EditMainDocument();
但我不想只打开它,但我要打印出MainDocument,这意味着如果我的mailmerge-datasource中有15个条目,我想打印15页。
当我perofrm wDoc.PrintOut我的基本文档文件被打印出来,没有填写数据!
这可能,以及如何?
到目前为止我的代码:
Application wApp = new Application();
Document wDoc = wApp.Documents.Open(ref oFile, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
wDoc.Activate();
wApp.ActivePrinter = "TestPrinter";
wDoc.MailMerge.OpenDataSource(wDoc.MailMerge.DataSource.Name, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
wDoc.MailMerge.Execute(ref missing);
wDoc.MailMerge.EditMainDocument();
wDoc.MailMerge.Execute(ref missing);
wDoc.PrintOut(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
wDoc.Close(ref saveOptionsObject, ref missing, ref missing);
wApp.Quit(ref saveOptionsObject, ref missing, ref missing);
答案 0 :(得分:1)
您需要以不同方式将文档发送到打印机。我正在尝试将VBA代码转换为C#,但可能还需要进行一些其他调整。
//before you run .Execute method try to set the followings:
wDoc.MailMerge.Destination = wdSendToPrinter; // vba constant
wDoc.MailMerge.SuppressBlankLines = True;
wDoc.MailMerge.DataSource.FirstRecord = wdDefaultFirstRecord; //vba constant
wDoc.MailMerge.DataSource.LastRecord = wdDefaultLastRecord; //vba constant
//and now you need to .Execute mailmerge
wDoc.MailMerge.Execute(ref missing); //or try with False parameter inside ()
编辑(测试C#,感谢Karl):
// ...OpenDataSource
wDoc.MailMerge.Destination = Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToPrinter;
wDoc.MailMerge.SuppressBlankLines = true;
wDoc.MailMerge.DataSource.FirstRecord = (int)Microsoft.Office.Interop.Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord;
wDoc.MailMerge.DataSource.LastRecord = (int)Microsoft.Office.Interop.Word.WdMailMergeDefaultRecord.wdDefaultLastRecord;
wDoc.MailMerge.Execute(ref missing);
// wDoc.Close...