Recent

Author Topic: How to print in certain printer [SOLVED]  (Read 3565 times)

derles

  • New member
  • *
  • Posts: 31
How to print in certain printer [SOLVED]
« on: October 31, 2017, 09:15:13 pm »
Hello.
I need to print a report on a certain printer that is not the default printer. I want to assign the printer by code, without using a printer selection form.
I know the name of the printer in windows, and I try to assign it before printing, but the printing still comes out in the printer by default.
My code:

Code: Pascal  [Select]
  1. Printers.Printer.SetPrinter (strMyPrinter);
  2. frReport.LoadFromFile (strMyReport);
  3. frReport.PrepareReport;
  4. frReport.PrintPreparedReport ('1-1', 1);
  5.  

What am I doing wrong ?
« Last Edit: November 01, 2017, 08:36:07 pm by derles »

GetMem

  • Hero Member
  • *****
  • Posts: 3476
Re: How to print in certain printer
« Reply #1 on: November 01, 2017, 05:36:02 am »
Hi derles,

Drop a combobox to your form, then try this:
Code: Pascal  [Select]
  1. uses Printers;
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. begin
  5.   Combobox1.Items := Printer.Printers;
  6.   if Combobox1.Items.Count > 0 then
  7.     Combobox1.ItemIndex := 0;
  8. end;
  9.  
  10. procedure TForm1.Button1Click(Sender: TObject);
  11. var
  12.   FromPage, ToPage, Copies: Integer;
  13.   OldPrinter: Integer;
  14. begin
  15.   FromPage := 1;
  16.   ToPage := frReport1.EMFPages.Count;
  17.   Copies := 1;
  18.   if Printer.PrinterIndex <> Combobox1.ItemIndex then
  19.     frReport1.ChangePrinter(Printer.PrinterIndex, Combobox1.ItemIndex);
  20.   if frReport1.PrepareReport then
  21.     frReport1.PrintPreparedReport(IntToStr(FromPage) + '-' + IntToStr(ToPage), Copies);
  22. end;
  23.  

This way you can select the appropriate printer from the list. You can achieve the same thing without a combobox, just loop though the printers list and locate the printer by name.

regards,
GetMem

derles

  • New member
  • *
  • Posts: 31
Re: How to print in certain printer
« Reply #2 on: November 01, 2017, 08:35:22 pm »
Thanks GetMem!

frReport1.ChangePrinter() do the trick, it's working now.

Solved !