【问题标题】:Printing Word document with NetOffice使用 NetOffice 打印 Word 文档
【发布时间】:2014-05-02 07:38:52
【问题描述】:

我正在将服务迁移到使用 NetOffice 而不是自动化 MS Word,以防止在部署到具有旧版 Office 的系统而不是在开发系统上时 Office 程序集版本不匹配。

到目前为止,一切都很顺利。

但是,我在打印 Word 文档时遇到了一些困难。这在自动化 MS Word 时工作得很好,但是当我尝试使用 NetOffice 时,我的代码中出现了转换错误。

这是我正在做的代码示例。 (appWord 是 NetOffice Word.Application 的一个实例)

                object paramFilePath = full_path_to_document;
                object falseValue = false;
                object missing = Type.Missing;
                object wb = appWord.WordBasic;
                int copies = 1;

                object[] argValues = null;
                string[] argNames = null;

                // the specific printer for the print job
                argValues = new object[] { "printer_name", 1 };
                // do not change the default printer
                argNames = new String[] { "Printer", "DoNotSetAsSysDefault" };

                Word.Document doc = appWord.Documents.Open(paramFilePath, missing, falseValue, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

                wb.GetType().InvokeMember("FilePrintSetup", BindingFlags.InvokeMethod, null, wb, argValues, null, null, argNames);

                for (int i = 0; i < copies; i++)
                {
                    appWord.PrintOut(); 

                    Thread.Sleep(100);
                }

这在 MS Word 上工作得很好(除了 Documents.Open 方法的参数是引用),但现在我在 object wb = appWord.WordBasic;。

谁能告诉我应该如何使用 NetOffice 在特定打印机上打印 Word 文档(同时不更改默认打印机),因为我没有成功迁移此特定方法。

【问题讨论】:

    标签: printing ms-word netoffice


    【解决方案1】:

    在 NetOffice 中获取 WordBasic-Object 时出现问题。

    此 C# 代码 (NetOffice 1.6.0) 可以在不更改系统默认打印机的情况下设置打印机:

    var dialog = appWord.Dialogs[WdWordDialog.wdDialogFilePrintSetup];
    
    var argValues = new object[] { "printer_name" };
    dialog.UnderlyingObject.GetType().InvokeMember("Printer", BindingFlags.SetProperty, null, dialog.UnderlyingObject, argValues);
    
    argValues = new object[] { 1 };
    dialog.UnderlyingObject.GetType().InvokeMember("DoNotSetAsSysDefault", BindingFlags.SetProperty, null, dialog.UnderlyingObject, argValues);
    
    dialog.Execute();
    

    【讨论】:

      猜你喜欢
      • 2014-04-11
      • 1970-01-01
      • 2012-04-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多