Search code examples
delphiprintingprinters

How do I get the PrinterIndex using the Printer Name using Delphi?


I need to assign a printer for each of three different printing functions, labels, receipts and "standard" (e.g. A4). I have identified all of the printers available using listbox1.assign(printer.printers) but there doesn't appear to be a way to use this to establish the printer's PrinterIndex. I want to store the printername and index value in a file so that I can use printer.printerindex to assign a printer to each type of print job without asking the user to choose a printer using a dialog.

Am I going about this the wrong way, and if so, could someone please tell me the right way to do it, please? I've not had to use the printers unit directly before.


Solution

  • Save the printer name in the file, not the index.

    When you restart the program and read back the printer name. Then loop thru all printers to find which one has the saved name (It could have been removed or renamed) and use that printer.