0

I'm on the middle of migrating a service to using NetOffice instead of automating MS Word, to prevent Office assembly version mismatch when deploying to systems with older Office than on the development system.

So far everything has worked out nicely.

However, I'm having some difficulties with printing out a Word document. This worked just fine when automating MS Word, but I'm getting cast errors in my code when I try to use NetOffice.

Here is a code sample of what I'm doing. (appWord is an instance of 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);
                }

This used to work just fine with MS Word (except that the paramters for the Documents.Open method was references), but now I'm getting a cast error at the line object wb = appWord.WordBasic;.

Can anyone tell me how I should go about printing out a Word document on a specific printer (while not changing the default printer) using NetOffice, because I'm not having any success migrating this specific method.

Kara
  • 6,115
  • 16
  • 50
  • 57
Aidal
  • 799
  • 4
  • 8
  • 33

1 Answers1

0

Theres a problem getting the WordBasic-Object in NetOffice.

This C#-Code works (NetOffice 1.6.0) to set the printer without changing the systems default printer:

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();