2014-05-02 105 views
0

我正在將服務遷移到使用NetOffice而不是自動化MS Word,以便在部署到具有較早Office的系統而不是開發系統上時防止Office程序集版本不匹配。使用NetOffice打印Word文檔

到目前爲止一切都很好。

但是,我在打印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方法paramters爲參考),但現在我得到的鑄錯誤在線對象wb = appWord.WordBasic;

任何人都可以告訴我如何使用NetOffice打印出特定打印機上的Word文檔(而不更改默認打印機),因爲我沒有成功遷移此特定方法。

回答

0

Theres在NetOffice中獲取WordBasic對象時出現問題。

這個C#-Code作品(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();