我正在将一个服务迁移到使用NetOffice,而不是自动化MS,以防止Office程序集版本错配时部署到旧的Office系统上,而不是在开发系统上。
到目前为止,一切都进展顺利。
然而,我在打印一个Word文档时遇到了一些困难。在自动化errors时,这种方法工作得很好,但当我尝试使用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中工作得很好(除了Documents.Open方法的参数是引用),但是现在我在行对象wb = appWord.WordBasic;上得到了一个强制转换错误。
有人能告诉我如何使用NetOffice在特定的打印机上打印Word文档(同时不更改默认的打印机),因为我没有成功地迁移这个特定的方法。
发布于 2015-04-02 12:07:38
在NetOffice中获取WordBasic对象有一个问题.
此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();https://stackoverflow.com/questions/23423129
复制相似问题