none
Print Specific PageRange & apply printer color mode by InterOp.Word in c# RRS feed

  • Question

  • Hello there,

    1.

    I'm facing problem while printing WORD document through Office.InterOp library.

    All thing is correct except 1, which is:

    when i give specific page range. I.e. 1-to-3 in my app which i'll need to set it up while calling doc.PrintOut(...) method. I get error of TYPE MISMATCH (which is very obvious for InterOps). But it occures only when parameters from & to is given some value. Here's a code snippet of my method.

    private async Task PrintDocFile(FileInformation fileInfo) { try { object missing = System.Reflection.Missing.Value; object copies = fileInfo.PrintOptions.NoOfCopies; object pages = string.Format("{0}-{1}", fileInfo.PrintOptions.PageFrom, fileInfo.PrintOptions.PageTo); object items = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent; object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages; object pageRange = fileInfo.PrintOptions.PageRange == PrintRange.AllPages ? Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument : Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintFromTo; object oFrom = fileInfo.PrintOptions.PageRange == PrintRange.SelectAll

    ? fileInfo.PrintOptions.PageFrom : missing; object oTo = fileInfo.PrintOptions.PageRange == PrintRange.Selection ? fileInfo.PrintOptions.PageTo : missing; object oTrue = true; object oFalse = false; object oCollate = fileInfo.PrintOptions.NoOfCopies > 1 ? true : false; object oDuplex = fileInfo.PrintOptions.PageHandling == System.Drawing.Printing.Duplex.Simplex ? false : true; string printerName = cmbPrinters.Text; await Task.Run(() => { Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = null; try { //Set no alerts as printing in background. word.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; //Set the printer. word.ActivePrinter = printerName; //Assign filename. doc = word.Documents.Open(fileInfo.FilePath, false, false, Visible: false); doc.Activate(); //Set Page Orientation. doc.PageSetup.Orientation = fileInfo.PrintOptions.Landscape ? Microsoft.Office.Interop.Word.WdOrientation.wdOrientLandscape : Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait; if (fileInfo.PrintOptions.PageRange == PrintRange.AllPages) { doc.PrintOut(ref oFalse, ref oFalse, ref missing, ref missing, ref missing, ref missing, ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oCollate, ref missing, ref oDuplex, ref missing, ref missing, ref missing, ref missing); } else {

    //ISSUE IS HERE, WITH PrintRange and From & To variables....... doc.PrintOut(ref oFalse, ref oFalse, Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintFromTo, Missing.Value, fileInfo.PrintOptions.PageFrom, fileInfo.PrintOptions.PageTo, ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oCollate, ref missing, ref oDuplex, ref missing, ref missing, ref missing, ref missing); } } catch (Exception ex) { throw ex; } finally { if (doc != null) { doc.Close(); } word.Quit(); } }); } catch (Exception ex) { throw ex; } }

    Please keep in mind FileInformation is custom class to keep file information like size, name, file pages etc. It also contains PrintOptions property which keeps individual file print settings.

    PrintOptions.From & PrintOptions.To are Int32 Properties which keeps from & to page numbers respectively.

    2.

    Another issue is i just can't find a way to set Print Color mode. Either Color or Monochrome. 

    Please help with the queries. any help would help me complete the task.

    Thanks in advance.!


    Wednesday, June 13, 2018 9:48 AM

All replies

  • Hi MayurPaghdal,

    Since your question is more related to word, I will move it to Word for Developers forum for suitable support.

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev&filter=alltypes&sort=lastpostdesc

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 14, 2018 6:14 AM
  • Hello MayurPaghdal,

    I would suggest you convert PrintOptions.From & PrintOptions.To to string format.

    Do a quick test if below code could work for you.

    doc.PrintOut(ref oFalse, ref oFalse, Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintFromTo, Missing.Value,
                                                         fileInfo.PrintOptions.PageFrom.ToString(), fileInfo.PrintOptions.PageTo.ToString(),
                                                         ref items, ref copies, ref pages, ref pageType, ref oFalse,
                                                         ref oCollate, ref missing, ref oDuplex,
                                                         ref missing, ref missing, ref missing, ref missing);

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 18, 2018 9:00 AM