[C#] Word指定印表機列印

  • 1930
  • 0

...

STP1. 專案>加入參考>.NET>Microsoft.Office.Interop.Word

 

STP2. VS2008


       public String strPrinter = "HP LaserJet 4100 Series PCL6";
       public String strFileName = @"D:\ADT\workspace\SNQueryWebService\SNQueryWebService\AspireE1-421AMDE11200B_BRNLED14WXGAG_BRSO2GBIII13_BRN500GB5.4KS_BR3rdWiFi1x1BGN_BR6CELL2.2_BRW8SL64.doc";

public string WORD()
        {
            try {

                Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
                wordApp.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
                wordApp.ActivePrinter = strPrinter;
               

                object objMissing = System.Reflection.Missing.Value;

                object objFleName = strFileName;   
                object confirmConversions = objMissing;
                object readOnly = objMissing;
                object addToRecentFiles = objMissing;
                object passwordDoc = objMissing;
                object passwordTemplate = objMissing;
                object revert = objMissing;
                object writepwdoc = objMissing;
                object writepwTemplate = objMissing;
                object format = objMissing;
                object encoding = objMissing;
                object visible = true;
                object openRepair = objMissing;
                object docDirection = objMissing;
                object notEncoding = objMissing;
                object xmlTransform = objMissing;

                object background = true;
                object append = false;
                object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
                object outputFileName = objMissing;
                object from = objMissing;
                object to = objMissing;
                object item = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
                object copies = "1";
                object pages = "";
                object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
                object printToFile = false;
                object collate = true;
                object activePrinterMacGX = objMissing;
                object manualDuplexPrint = false;
                object printZoomColumn = objMissing;
                object printZoomRow = objMissing;
                object printZoomPaperWidth = objMissing;
                object printZoomPaperHeight = objMissing;

                object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
                object originalFormat = objMissing;
                object routeDocument = objMissing;

                Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Open(
                    ref objFleName, ref confirmConversions, ref readOnly, ref addToRecentFiles,
                    ref passwordDoc, ref passwordTemplate, ref revert, ref writepwdoc,
                    ref writepwTemplate, ref format, ref encoding, ref visible, ref openRepair,
                    ref docDirection, ref notEncoding, ref xmlTransform);

                wordApp.ActiveDocument.PrintOut(
                    ref background, ref append, ref range, ref outputFileName, ref from,
                    ref to, ref item, ref copies, ref pages, ref pageType,
                    ref printToFile, ref collate, ref activePrinterMacGX, ref manualDuplexPrint, ref printZoomColumn,
                    ref printZoomRow, ref printZoomPaperWidth, ref printZoomPaperHeight);

                wordDoc.Close(ref saveChanges, ref originalFormat, ref routeDocument);

                // Make sure all of the documents are gone from the queue
                while (wordApp.BackgroundPrintingStatus > 0)
                {
                    System.Threading.Thread.Sleep(250);
                }

                wordApp.Quit(ref saveChanges, ref originalFormat, ref routeDocument);

                return "OK";
            }
            catch (Exception e)
            {
                return e.ToString();
            }
 
        }