我想打印出我在程序中生成的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);
}
}
我第一次打印文档时,它的工作方式就像它应该的那样,但之后请求被发送到打印机,没有任何反应。
我做错了吗?是否有更好的方法来实现这一目标?
答案 0 :(得分:1)
我认为问题是在打印完成之前文档已关闭。
我在文档关闭之前添加了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();
答案 1 :(得分:1)
您不允许该过程完成打印。为此,您需要暂停代码,为此,您可以使用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
“后台确实可以在Microsoft Office Word打印文档时继续自定义代码。”