首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Word自动化打印现有文档

使用Word自动化打印现有文档
EN

Stack Overflow用户
提问于 2013-06-14 22:27:07
回答 2查看 813关注 0票数 0

我想打印出我在程序中生成的word文档。因此,我使用以下代码:

代码语言:javascript
复制
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);
    }
}

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

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

EN

回答 2

Stack Overflow用户

发布于 2013-11-14 22:20:31

我认为问题在于文档在打印完成之前就被关闭了。

我在文档关闭前添加了WHILE:

代码语言:javascript
复制
 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
EN

Stack Overflow用户

发布于 2014-09-19 18:23:44

您不允许进程完成打印。要做到这一点,你需要暂停你的代码,你可以使用PrintOut的第一个参数。

代码语言:javascript
复制
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打印文档时继续执行自定义代码。“

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17110802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档