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...

Hi All,

I figured it out I should have been using Integers instead of Longs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.