1

EDIT: This was fixed by handling the background printing better. The linked solution has a way to continually sleep until the background work is done.

This is going to be a bit hard to explain and show (because there are thousands of lines of code). Basically, the company I work at uses a program to print out letters, update SQL records, etc. The letters are populated using Bookmarks and SQL data. The program that's being used currently is quite literally 15+ years old, and I have been tasked with re-writing it in VB.Net from VB6. Everything has honestly been going great, and the program is basically done.

The only thing I'm having trouble with is printing a specific letter from a specific printer. The letter is an 8.5x14, and it's the only letter of its size so it's a bit difficult to test (although I've used different versions of the same letter, and the result is the same - won't print).

The code I'm using has no problem printing out letters of different sizes on different printers, but something specifically with that printer isn't working with the code. I've also tried printing the document directly without using the program (through Word and PDF) and it prints fine.

Here is the printout code:

goWord.Visible = True
MsgBox("Click OK when finished making changes.")

goWord.WordBasic.fileprintsetup(Printer:=strLetterPrinter, DoNotSetAsSysDefault:=1)

If intTraySelection <> 0 Then
    goWord.ActiveDocument.PageSetup.FirstPageTray = intTraySelection
Else
    goWord.ActiveDocument.PageSetup.FirstPageTray = 261
End If

goWord.ActiveDocument.PrintOut()
System.Threading.Thread.Sleep(500)

odoc.Close(False)
odoc = Nothing

goWord.Quit(False)

odoc = Nothing
goWord = Nothing

The intTraySelection numbers are pulled directly from the old code (which WILL print this exact same letter), but I'm not sure if they're correct. Here is the code that determines what the number is (based on a radio button the user selects):

If rdbTray1.Checked = True Then
    intTraySelection = 258
ElseIf rdbTray2.Checked = True Then
    intTraySelection = 260
ElseIf rdbTray3.Checked = True Then
    intTraySelection = 259
ElseIf rdbTray4.Checked = True Then
    intTraySelection = 257
Else
    intTraySelection = 261
End If

I'm assuming the numbers mean something, but I'm unable to find anything online. Regardless, the result is the same if I don't set the .FirstPageTray property at all. All other letters on different printers print fine, but this one doesn't.
Just to confirm, the problem is not that the ActiveDocument doesn't open or anything like that. I have it opening for them to make changes if needed, and I can see that it's the correct letter. I can even print it directly from the Word doc that pops-up (before hitting OK on the message box) and it prints fine. Something about telling it to .PrintOut automatically doesn't work.

I'm also watching the Printer Status screen. Printing from Word does the typical Spooling --> Printing --> Done sequence. Printing automatically from the program goes to Spooling, but never prints and just cancels the job.

Let me know if I've missed anything or need to provide more info.

  • `but I'm not sure if they're correct` - well, verify that. Put a breakpoint at `If intTraySelection <> 0 Then` and examine `intTraySelection`, then do the same in the VB6 version. And, does the VB6 version also have a `Sleep()`? – GSerg Jul 15 '21 at 17:34
  • @GSerg Sorry, I meant that the code is correctly setting the intTraySelection variable, but I have no idea what those numbers mean. I can't find any info online about it, but they seem oddly specific. The old program does not have sleep, but if I don't have sleep, it works too fast and closes the doc before it finishes processing the print. – Daniel Long Jul 15 '21 at 17:38
  • @GSerg I just changed the sleep to 5 seconds, and with that letter/printer that won't print, I at least got to Spooling --> Printing on the status screen. It then gives an "error", but the error just says the job created an error. I can't find any error logs or anything for it either. – Daniel Long Jul 15 '21 at 17:40
  • https://stackoverflow.com/questions/35195769/selected-printer-tray-does-not-print-word-document#comment58119699_35195769? – GSerg Jul 15 '21 at 17:40
  • I didn't mean to make the sleep longer. Remove it completely. – GSerg Jul 15 '21 at 17:41
  • @GSerg Right, but like I said, without sleep in there, it works too fast and just doesn't print out. I actually just solved the problem. It won't print if I do sleep(500), it won't print if I do sleep(5000), but it works perfectly if I do sleep(3000).... I really don't understand that. – Daniel Long Jul 15 '21 at 17:55
  • Apparently after that code you have different code that you haven't shown, that terminates the printing. – GSerg Jul 15 '21 at 17:56
  • @GSerg Yes, I do. I didn't think it'd be relevant since it's just closing out things after it's all finished, but it appears I was wrong. I've added that code in my most recent edit. Brings up an interesting point, though; am I closing things out the wrong way, and that's why I'm needing to sleep and such? – Daniel Long Jul 15 '21 at 18:04
  • @GSerg I would say yes, even though it's C#. They're similar enough. That looks like a better way to do it than just sleeping and guessing when it will be done. Thanks! – Daniel Long Jul 15 '21 at 18:37

0 Answers0