0

我正在将服务迁移到使用 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 文档,因为我没有成功迁移此特定方法。

4

1 回答 1

0

在 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();
于 2015-04-02T12:07:38.580 回答