I get errors like “List index out of bounds” when sending a QuickReport report to the selected printer as the Printer.Printers stringlist is accessed.
exception class : EStringListError
exception message : List index out of bounds (6).
main thread ($1594c):
00479559 skdata.exe Classes 5060 TStringList.GetObject
004a258b skdata.exe Printers 581 TPrinter.GetPrinter
007ca744 skdata.exe QRPrntr 3208 TPrinterSettings.ApplySettings
007cb5bb skdata.exe QRPrntr 3995 TQRPrinter.BeginDoc
007be227 skdata.exe QuickRpt 4645 TCustomQuickRep.CreateReport
007be909 skdata.exe QuickRpt 4853 TCustomQuickRep.Print
00859bc7 skdata.exe PostLst 142 TPostSpecListReport.Print
I’ve added logging that shows that the list of printers (Printer.Printers) has changed just before the error happened. I guess the list of printers can change if a network printer is switched off, if the network is down, and for other reasons too.
How to handle this situation? In Delphi the selected printer is given by the Printer.PrinterIndex, but if the list of printers is changed, then this index is not valid anymore.
I can only think of one solution which is to present the select-printer dialog before every print, in other words not to rely in Printer.PrinterIndex being correct from one print job to the next. But that could be quite annoying to the user.
Ideally the selected printer should be stored as a GUID or by name. That would make it more reliable.
I’ve searched this forum as well as the Embarcadero forum but haven’t found any mention of this kind of problem. So maybe there’s something simple here that I’ve overlooked?
I use Delphi 2007 and QuickReport 5.
You can store the printer name, if you’d like, and assign it in the
OnBeforePrintevent handler or in the constructor of the report. If you don’t want to hard-code it, store it in the registry or an ini file.