How can I Open PDF files (window default program Adobe Reader) through Delphi 2009?

Trevor picture Trevor · Jun 1, 2012 · Viewed 16.4k times · Source

So basically when I try to open up PDF files that are windows defaulted to open with Adobe Reader nothing happens. If I set the default program to Internet Explorer it works..

Here is my code

var
openDialog : TOpenDialog;    // Open dialog variable
begin
  openDialog := TOpenDialog.Create(self);
  openDialog.InitialDir := MaskEditLocation.Text;
  if openDialog.Execute then
   ShellExecute(Handle, PChar('Open'), PChar(openDialog.FileName), nil, nil,
   SW_SHOWNORMAL);
  openDialog.Free;
end;

Any Ideas?

Thanks for the help!

Answer

Ken White picture Ken White · Jun 1, 2012

You should never presume that the application has registered a specific verb like open or run. Just leave the verb empty when you want the default behavior, and let Windows decide:

ShellExecute(Handle, nil, PChar(OpenDialog.FileName), nil,  nil, SW_SHOWNORMAL);