none
Selecting printer Trays in Word 2010 with Interop RRS feed

  • Question

  • Hi All,

    I have an application written in VB.net for batch print jobs.  As part of the application, the user selects two printer trays to use to print the batch to (one plain paper the other coloured for coversheets and batch seperators) I had this working until we upgraded our systems from Windows XP with Office 2000 to Windows 7 and Office 2010. Now the selected trays are not being recognised and an error is thrown.

    I've looked at similar posts on here and other forums but they seam to concentrate on using some word default trays (wdPrinterDefaultBin etc) however I need to be able to let the user pick whatever tray they wish to use. I don't want to have to go through every printer in the building and map the trays to the word values either as I don't want to rewrite the app everytime we've a new printer.

    Here is the code:

    sub PopulatePrinterTray (byref Printer as String)
    'gets printer trays for selected printer and then populates some comboboxes
    Dim pkSource As Printing.PaperSource
    Dim printDoc As New Printing.PrintDocument
    
    'Clear the comboboxes
     cbDocTray.Items.Clear()
     cbRedTray.Items.Clear()
    'take printer paper sources i.e. trays
     printDoc.PrinterSettings.PrinterName = Printer
    For Each pkSource In printDoc.PrinterSettings.PaperSources
      'dataitem custom class holds a ID and a string (displays string)
       cbDocTray.Items.Add(New DataItem(pkSource.RawKind, pkSource.SourceName))
       cbRedTray.Items.Add(New DataItem(pkSource.RawKind, pkSource.SourceName))
    next
    end sub
    
    Sub PrintCover(byref MyCoversheet as String, byref Printer as String, byref RedTray as Long)
    Dim oWord As Object = CreateObject("Word.Application")
    Dim oDoc As Object = oWord.Documents.open(MyCoversheet)
    .....
    
    If InStr(oWord.ActivePrinter, Printer) = 0 Then
          With oWord.Dialogs.Item(wdDialogFilePrintSetup)
            .printer = Printer
            .donotsetassysdefault = True
            .Execute()
          End With
    End If
    
     oDoc.PageSetup.FirstPageTray = RedTray 'Error occurs here
        oDoc.PageSetup.OtherPagesTray = RedTray
        oDoc.PrintOut(Range:=wdPrintAllDocument, Item:=wdPrintDocumentContent, Copies:=1)
        System.Threading.Thread.Sleep(1000)
    end sub
    
    As can be seen from above the error happens when setting the tray. There must be a way to do this as Word gives you the printer trays regardless of printer...

     

    Tuesday, January 4, 2011 3:16 PM

Answers

  • Hello gwaddell,

    Thanks for posting. From what you said, an error occurs at the PageSetup.FirstPageTray line. Would you please tell us the exact error?

    However, I think you could try to put the code into try and catch block and debug it step by step. Then you will see the value of RedTray which has an error on that line. Now you have got the value of RedTray. Please test it on another simple project, and see if this error also happens.

    Also here is a thread about printer trays. Hope it could help you more or less.

    Programmatically changing printer paper trays:
    http://social.msdn.microsoft.com/Forums/en/vsto/thread/c6a702e7-8ade-483d-968f-e1c1b20642a7.

    If you have any concern on this post, just feel free to follow up. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bessie Zhao Tuesday, January 11, 2011 3:16 AM
    Wednesday, January 5, 2011 3:51 AM
  • Hi

    Sorry I thought I had replied to the thread, it turned out instead of passing in a LONG I should have been passing in an INTEGER. Word 2010 must be more sensitive to the datatype.

    • Marked as answer by Bessie Zhao Thursday, February 10, 2011 9:58 AM
    Monday, January 24, 2011 12:10 PM

All replies

  • Hello gwaddell,

    Thanks for posting. From what you said, an error occurs at the PageSetup.FirstPageTray line. Would you please tell us the exact error?

    However, I think you could try to put the code into try and catch block and debug it step by step. Then you will see the value of RedTray which has an error on that line. Now you have got the value of RedTray. Please test it on another simple project, and see if this error also happens.

    Also here is a thread about printer trays. Hope it could help you more or less.

    Programmatically changing printer paper trays:
    http://social.msdn.microsoft.com/Forums/en/vsto/thread/c6a702e7-8ade-483d-968f-e1c1b20642a7.

    If you have any concern on this post, just feel free to follow up. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bessie Zhao Tuesday, January 11, 2011 3:16 AM
    Wednesday, January 5, 2011 3:51 AM
  • Hello again gwaddell,

    I am writing to check the status of the issue on your side. Could you please let me know if the suggestion works for you or not? If you have any questions or concerns, please feel free to let me know, or unmark it back. Have a nice day


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 11, 2011 3:15 AM
  • Hi

    Sorry I thought I had replied to the thread, it turned out instead of passing in a LONG I should have been passing in an INTEGER. Word 2010 must be more sensitive to the datatype.

    • Marked as answer by Bessie Zhao Thursday, February 10, 2011 9:58 AM
    Monday, January 24, 2011 12:10 PM