I recently changed the printing code for automated printing to a TPrinter based solution. (The old solution, writing text directly to the printer network device worked without errors for many years.)
The application is written in Delphi 2009 Enterprise and runs continuously in a user session (not as a service). In regular intervals, the application scans a database for new documents, creates the text to be printed, and sends it to a printer.
Before printing, the code sets the printer index:
PrinterToUse := Printer.Printers.IndexOf(MyPrinterName);
if PrinterToUse > -1 then
begin
Printer.PrinterIndex := PrinterToUse;
end
else
begin
Assert(False, Format('Printer %s not found', [MyPrinterName]));
end;
Where MyPrinterName could be for example \\PRINTSERVER\PR001
. For my test phase, I use one locally installed printer with the new printing method, so the printer name is always the same.
In random intervals, printing fails with the EPrinter exception 'Printer selected is not valid'.
If this exception occurs, printing of the document fails - I added a workaround which uses the 'legacy' print method when this happens.
I suspect that there could be bugs in the VCL printers unit. In Quality Central I found only one related entry - http://qc.embarcadero.com/wc/qcmain.aspx?d=127390. I could try a newer Delphi version, if the problem remains it could be a printer driver issue. Updating printer drivers is also an option, which I have to try.
I will also improve error handling, and re-try printing one or two times when the error occurs.
Are there other experiences with this random error and has there been a solution?