0

我想打印出我在程序中生成的 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);
    }
}

我第一次打印文档时它的工作方式与它应该的一样,但之后请求被发送到打印机并且没有任何反应。

我做错什么了吗?有没有更好的方法来实现这一点?

4

2 回答 2

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();
于 2013-11-14T14:20:30.750 回答
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 打印文档时继续自定义代码。”

于 2014-09-19T10:23:44.473 回答