• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Detecting default printer !

How can I detect if there is a default printer selected ?
I don't need to know if there is a printer installed just if it there is a DEFAULT printer selected.
0
sile
Asked:
sile
  • 2
  • 2
1 Solution
 
sperlingCommented:
This function will return the name of the default printer, or an empty string if there are no default.

uses
  Printers;

function DefaultPrinter : STRING;
begin
  Printer.PrinterIndex := -1;
  if (Printer.PrinterIndex < 0)
  or (Printer.PrinterIndex >= Printer.Printers.Count)
  then Result := ''
  else Result := Printer.Printers[Printer.PrinterIndex];
end;

Regards,

Erik.
0
 
sileAuthor Commented:
Now I have tried the code but it doesn't work if there is no printer installed ... In this case my app. terminates and I get an error message saying No default printer currently sellected ?
Can you make it work in this case ?

0
 
sperlingCommented:
Hmmm... Try this one.


function DefaultPrinter : STRING;
begin
  if (Printer=nil) or (Printer.Printers=nil)
  or (Printer.Printers.Count = 0)
  then begin
    Result := ''
  end else begin
    try
      Printer.PrinterIndex := -1;
      Result := Printer.Printers[Printer.PrinterIndex];
    except
      on Exception do Result := '';
    end;
  end;
end;


Regards,

Erik.
0
 
sileAuthor Commented:
Ok THANX it works now.
0
 
rwilson032697Commented:
Bought this Q...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now