Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shellexecute PrintDialog(delphi)

Status
Not open for further replies.

delphitask

Programmer
Oct 10, 2009
3
0
0
GB
Could anyone please tell me why print using Shellexecute does not bring up the PrintDialog. This is the command used.
Shellexecute(0, 'Print', pchar(Filename), '', nil, SW_SHOW);
It prints directly to the default printer. We Need to print pdf ,doc,html,xls files and need to give user ability to choose printer.
 
Because using ShellExecute sends the action to the program, whose sole responsibility as delegated through Windows Explorer is to print the document, so it does that. And to eliminate the question of where it goes, the default printer is chosen.

It seems you need to seek different means to accomplish this.

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
Read about Shellexecute with the Printto option(instead of print) . I tried it- but nothing gets printed. Any help from those of you who have tried . This is what I used to select a printer of my choice. Any suggestions would be helpful

procedure DocPrinting(DocName: string);
var
Device: array[0..255] of Char;
Driver: array[0..255] of Char;
Port: array[0..255] of Char;
S: string;
hDeviceMode: THandle;
PrintDialog: TPrintDialog;
begin
PrintDialog:=TPrintDialog.create(nil);
If PrintDialog.Execute then
begin
Printer.PrinterIndex := Printer.Printers[Printer.PrinterIndex];

Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
S := Format('"%s" "%s" "%s"', [Device, Driver, Port]);
ShellExecute(0, 'print', PChar(DocName), S, nil, SW_HIDE);
end;
end;
 
What you posted basically works except for a couple of problem issues.

Code:
procedure DocPrinting(DocName: string);
  var
    Device: array[0..255] of Char;
    Driver: array[0..255] of Char;
    Port: array[0..255] of Char;
    S: string;
    hDeviceMode: THandle;
    PrintDialog: TPrintDialog;
  begin
    PrintDialog := TPrintDialog.Create(nil);
    try
      If PrintDialog.Execute then
        begin
          Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
          S := Format('"%s" "%s" "%s"', [Device, Driver, Port]);
          ShellExecute(0, 'printto', PChar(DocName), PChar(S), nil, SW_HIDE);
        end;
    finally
      PrintDialog.Free;
    end;
  end;

For what you might notice, the "printto" command within Windows Explorer might not be supported for the document(s) you are trying to print (ShellExecute is a Windows Explorer shell function) - "printto" is not standard and doesn't appear in the MSDN documentation for ShellExecute ( - though it could probably just be old). This means if any app supports it, it's probably a vendor extension. If what was posted above doesn't work, check the registry of the computer you are on to see if a printto command is defined.

It also probably is pretty prudent to ask what OS and Delphi you are trying this with.

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top