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.