我想打印出我在程序中生成的word文档。因此,我使用以下代码:
public static void druckeRechnung(object oFilename)
{
object oMissing = System.Reflection.Missing.Value;
List<int> procIds = getRunningProcesses();
_Application wordApp = new Application();
wordApp.Visible = false;
_Document aDoc = wordApp.Documents.Add(ref oFilename);
try
{
System.Windows.Forms.PrintDialog pDialog = new System.Windows.Forms.PrintDialog();
if (pDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
wordApp.ActivePrinter = pDialog.PrinterSettings.PrinterName;
wordApp.ActiveDocument.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "Fehler beim Drucken");
}
finally
{
aDoc.Close(WdSaveOptions.wdDoNotSaveChanges);
wordApp.Quit(WdSaveOptions.wdDoNotSaveChanges);
aDoc = null;
wordApp = null;
killProcess(procIds);
}
}当我第一次打印一个文档时,它的工作方式是正常的,但是之后,请求被发送到打印机,没有任何反应。
我做错什么了吗?有没有更好的方法来实现这一点?
发布于 2013-11-14 22:20:31
我认为问题在于文档在打印完成之前就被关闭了。
我在文档关闭前添加了WHILE:
var printDialog = new System.Windows.Forms.PrintDialog();
if (printDialog.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
w. = printDialog.PrinterSettings.PrinterName;
d.PrintOut();
}
while (w.BackgroundPrintingStatus>0)
{
}
d.Close(false);
w.Quit();发布于 2014-09-19 18:23:44
您不允许进程完成打印。要做到这一点,你需要暂停你的代码,你可以使用PrintOut的第一个参数。
object background = false;
wordApp.ActiveDocument.PrintOut(background, 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);正如文档所说:http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.printout(v=vs.80).aspx
"Background true可在Microsoft Office Word打印文档时继续执行自定义代码。“
https://stackoverflow.com/questions/17110802
复制相似问题