Ejemplo n.º 1
0
 public void PrintWord(string file, string printer)
 {
     try
     {
         // Kill opened word instances.
         if (KillProcess("WINWORD"))
         {
             // Thread safe.
             lock (locker)
             {
                 string fileName    = file;
                 string printerName = printer;
                 if (File.Exists(fileName))
                 {
                     Microsoft.Office.Interop.Word.Application _application = new Microsoft.Office.Interop.Word.Application();
                     _application.Application.ActivePrinter = printerName;
                     object   oSourceFilePath = (object)fileName;
                     object   docType         = WdDocumentType.wdTypeDocument;
                     object   oFalse          = (object)false;
                     object   oMissing        = System.Reflection.Missing.Value;
                     Document _document       = _application.Documents.Open(ref oSourceFilePath,
                                                                            ref docType,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing,
                                                                            ref oMissing);
                     // Print
                     _application.PrintOut(ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                           ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                           ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                     object saveOptions = WdSaveOptions.wdDoNotSaveChanges;
                     _document.Close(ref oFalse, ref oMissing, ref oMissing);
                     if (_application != null)
                     {
                         object oSave = false;
                         Object oMiss = System.Reflection.Missing.Value;
                         _application.Quit(ref oSave, ref oMiss, ref oMissing);
                         _application = null;
                     }
                     // Delete the file once it is printed
                     File.Delete(fileName);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         KillProcess("WINWORD");
         MessageBox.Show(ex.Message.ToString(), "Error while Printing - Student Management System", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     finally
     {
     }
 }