应用邮件合并后无法打印文档
Cannot print document after mail merging is applied
我尝试在应用邮件合并后打印文档,但出现以下错误:
cannot implicitly convert type 'microsoft.interop.word.document' to 'system.drawing.printing.printdocument'
代码如下:
private void btnSave_Click(object sender, EventArgs e)
{
// Open the database connection.
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\_Employees\Employee_Database\Database From Excel.accdb";
OleDbConnection conn = new OleDbConnection(connString);
try
{
conn.Open();
// Get data from a database.
OleDbCommand cmd = new OleDbCommand("SELECT * FROM EmpInfo", conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
System.Data.DataTable data = new System.Data.DataTable();
da.Fill(data);
// Open the template document.
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(@"c:\_Employees\Template\MergeDoc.docx");
System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
// Loop though all records in the data source.
foreach (DataRow row in data.Rows)
{
// Execute mail merge.
doc.MailMerge.Execute(row);
docToPrint = (PrintDocument)doc;
DialogResult result = printDialog1.ShowDialog();
// If the result is OK then print the document.
if (result == DialogResult.OK)
{
docToPrint.Print();
}
}
稍作修改后,没有错误,但页面不打印。
好像是什么问题?
当您使用 "Word interop" 时,您必须通过 Word 应用程序界面进行打印。只有 Word 真正知道如何解释 Word 内容(XML 和其他文件的 Zip 包)以供打印。 .NET Framework 的 System.Drawing.Printing 无法处理 Word 文档。
因此,正如错误消息告诉您的那样,您不能将 Word.Document 对象转换为 System.Drawing.Printing.PrintDocument。
可以使用Word对象模型的Document.Print命令,如果需要设置份数等,可以用它的参数。以你的代码为基础:doc.Print();
我不知道您的 PrintDialog 是做什么用的,所以无法说明如何将它与打印 Word 文档联系起来...
我尝试在应用邮件合并后打印文档,但出现以下错误:
cannot implicitly convert type 'microsoft.interop.word.document' to 'system.drawing.printing.printdocument'
代码如下:
private void btnSave_Click(object sender, EventArgs e)
{
// Open the database connection.
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\_Employees\Employee_Database\Database From Excel.accdb";
OleDbConnection conn = new OleDbConnection(connString);
try
{
conn.Open();
// Get data from a database.
OleDbCommand cmd = new OleDbCommand("SELECT * FROM EmpInfo", conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
System.Data.DataTable data = new System.Data.DataTable();
da.Fill(data);
// Open the template document.
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(@"c:\_Employees\Template\MergeDoc.docx");
System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
// Loop though all records in the data source.
foreach (DataRow row in data.Rows)
{
// Execute mail merge.
doc.MailMerge.Execute(row);
docToPrint = (PrintDocument)doc;
DialogResult result = printDialog1.ShowDialog();
// If the result is OK then print the document.
if (result == DialogResult.OK)
{
docToPrint.Print();
}
}
稍作修改后,没有错误,但页面不打印。 好像是什么问题?
当您使用 "Word interop" 时,您必须通过 Word 应用程序界面进行打印。只有 Word 真正知道如何解释 Word 内容(XML 和其他文件的 Zip 包)以供打印。 .NET Framework 的 System.Drawing.Printing 无法处理 Word 文档。
因此,正如错误消息告诉您的那样,您不能将 Word.Document 对象转换为 System.Drawing.Printing.PrintDocument。
可以使用Word对象模型的Document.Print命令,如果需要设置份数等,可以用它的参数。以你的代码为基础:doc.Print();
我不知道您的 PrintDialog 是做什么用的,所以无法说明如何将它与打印 Word 文档联系起来...