Click here to Skip to main content
15,918,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
VB
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...
Posted
Comments
Prerak Patel 7-Jan-11 5:00am    
What error you get?!
George Waddell 7-Jan-11 5:04am    
I got one of those indistinct HResult Com object errors....
I have however just found the solution, instead of using the Type Long for the Trays I changed to Integer and it all worked.

1 solution

Resolution:
Word 2010 is expecting an integer rather than a Long which makes me think I maybe should have been passing Integer values all a long.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900