Solved

Get Default Printer

Posted on 2006-11-17
3
1,823 Views
Last Modified: 2012-06-27
Hi Experts,

Can anybody show me code line(s) how to get the default printer in delphi 6?
Say provided a form and a button, when the user clicks on it, it will
display the default printer (on the machine where this program is running)
together with the default paper size and paper length.
Note that from time to time, the default printer may not be the one locally attached on this computer. It might be from another machine/computer.

TIA,
yorge
0
Comment
Question by:yorge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
atul_parmar earned 75 total points
ID: 17963925
Very easy,

uses
  printers;

....

begin
  ShowMessage(Printer.Printers[Printer.PrinterIndex]);
end;

to know the paper size use Printer.Paperwidth and Paperheight properties.
0
 
LVL 10

Assisted Solution

by:atul_parmar
atul_parmar earned 75 total points
ID: 17963968
more precisely,
Printer.PrinterIndex := -1; // sets the printer to default; -1 for default printer
then
ShowMessage(Printer.Printers[Printer.PrinterIndex]);
...



0
 
LVL 1

Expert Comment

by:PaulShearing
ID: 23872210
Thanks atul. I have found that without first setting the default printer a reference to Printer.PrinterIndex can cause a runtime crash deep in ntdll.dll. Without setting the PrinterIndex to -1 my code would fail on the line:

If (Printer.PrinterIndex > 0) Then

My Delphi code to get the default printer name now looks like this:

Function getDefaultPrinterName(): String;
Begin
   Printer.PrinterIndex := -1;     // set to default
   If (Printer.PrinterIndex > 0) Then
      Try
         Result := Printer.Printers[Printer.PrinterIndex]
      Except
         Result := 'Unknown'
      End
   else
      Result := 'Unknown'
End;


0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question