none
How set printer to duplex when printing using ...InterOp.Word

    Question

  • Hi,

    I have code that uses:

    Microsoft.Office.Interop.Word;

    and allows me to programmatically decide which printer tray to print page one and/or the rest of the document.

    However I am unable to set the whether the pages should be printed duplex (both the trays I am using allow this fnction). The pages always come out NON duplex

    This is my code:

    using WORD = Microsoft.Office.Interop.Word;

    ...

    public static void PrintDocTest()

    {

    object oMissing = (object)System.Reflection.Missing.Value;

    object template = (object)@"[Path to template][Template].dot";

    object readOnly = (object)false;

    object isVisible = (object)false;

    object SaveChanges = (object)false;

    object OriginalFormat = (object)WORD.WdOriginalFormat.wdWordDocument;

    object RouteDocument = (object)false;

    WORD._Application oWordApp = null;

    WORD._Document oWordDoc1 = null;

    string ActivePtr = null;

    try

    {

    oWordApp = new WORD.Application();

    // Open template

    oWordDoc1 = oWordApp.Documents.Open(ref template, ref oMissing,

    ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

    ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing,

    ref oMissing, ref oMissing);

    oWordDoc1.Activate();

    ...

    // Add some text at various bookmarks embedded in the template

    object FileNameAdd = (object)@"[Path to file][Filename].doc";

    object ReadOnlyRecommended = (object)false;

    oWordDoc1.SaveAs(ref fileNameAdd, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

    ref oMissing, ref ReadOnlyRecommended, ref oMissing, ref oMissing, ref oMissing,

    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    ActivePtr = oWordApp.ActivePrinter;

    if ((ActivePtr != null) && (ActivePtr.Trim().ToLower().Equals(@"The correct printer")))

    {

    int x = 1;

    object Copies = (object)x;

    // **** This page SHOULD not be duplex ***

    oWordDoc1.PageSetup.FirstPageTray = (Microsoft.Office.Interop.Word.WdPaperTray)260; // Tray 2

    // At this point I want to be able to set the printer to duplex printing

    // *** These pages should be duplex ***

    oWordDoc1.PageSetup.OtherPagesTray = (Microsoft.Office.Interop.Word.WdPaperTray)261; // Tray 3

    oWordApp.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

    ref oMissing, ref oMissing, ref Copies, ref oMissing, ref oMissing, ref oMissing,

    ref oMissing, ref fileNameAdd, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

    ref oMissing, ref oMissing);

    }

    else

    {

    System.Windows.Forms.MessageBox.Show("Unable to find correct printer");

    }

    }

    catch (Exception e)

    {

    if (oWordDoc1 != null)

    {

    oWordDoc1.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);

    oWordDoc1 = null;

    }

    if (oWordApp != null)

    oWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);

    }

    Util.CollectGarbage();

    }

    ...

    In the code above you will see that I set the out put trays of the printer and get and on some occassions set the default printer - all that works fine.

    However after printing page one I want to print the rest of the document as duplex - This DOES NOT work O.K.

    If I cannot do it between trays can I at least do it for each document? The printing process will be run by users whose normal set up for this printer will not require duplex.

    Any help greatly appreciated.

    Thanks Andrew

     

    Monday, October 16, 2006 11:03 AM

Answers

  • Microsoft has changed Word API, they removed the duplex property of the printer. Word does provide a parameter in WordApplication class’ PrintOut method, but Microsoft suggests not to use as it is OS and language dependent.

    Here is the full article and full working printer library developed by a frustrated developer that had the same issue: http://www.codeproject.com/dotnet/NET_Printer_Library.asp

    Monday, October 16, 2006 11:17 AM

All replies

  • Microsoft has changed Word API, they removed the duplex property of the printer. Word does provide a parameter in WordApplication class’ PrintOut method, but Microsoft suggests not to use as it is OS and language dependent.

    Here is the full article and full working printer library developed by a frustrated developer that had the same issue: http://www.codeproject.com/dotnet/NET_Printer_Library.asp

    Monday, October 16, 2006 11:17 AM
  • Hi,

    Thanks - however this seems a very involved way of solving what I was hoping was only a simple problem.

    I need my process to be quick (as it is for batch printing) and also ensure the print settings on the users machine is the same at the end of the batch print process as it was before the process started (ie only alter it for each document, groups of documents or set of pages).

    Is there another way this could simply be achieved?

    For example:

    1. Create the word document using ...InterOp.Word

    2. Save the document with the ammendments.

    3. Print document (NOT Using InterOp.Word)

    I am also printing using System.Diagnostics.Process

    However again there does not seem to be a clear way to control trays, duplex etc...

    Any other ideas please.

    Thanks Andrew

     

    Monday, October 16, 2006 1:33 PM
  • No, there is no easier way than the print library i provided in my previous post. Don't worry about speed, because the printer is allways the bottleneck. So optimizing your process won't be noticable in the full batch process because it is allways the printing process that will slow everything down.
    Monday, October 16, 2006 1:39 PM