【问题标题】:Print existing document with Word automation使用 Word 自动化打印现有文档
【发布时间】:2013-06-14 14:27:06
【问题描述】:

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

我第一次打印文档时,它的工作正常,但之后请求被发送到打印机,没有任何反应。

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

【问题讨论】:

    标签: c# automation ms-word word-automation


    【解决方案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();
    

    【讨论】:

    • 感谢您的回答。自从我从事这个项目以来已经有一段时间了,但我一定会尝试你的方法!
    【解决方案2】:

    您不允许该过程完成打印。为此,您需要暂停代码,为此您可以使用 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 打印文档时继续自定义代码。”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 2014-04-11
      • 1970-01-01
      • 2021-07-15
      • 2012-04-01
      • 2023-04-09
      相关资源
      最近更新 更多