3

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!

Trevor
  • 16,080
  • 9
  • 52
  • 83
  • Long ago, I remember Acrobat Reader not working if `ShellExecute` was called while the application was being debugged, but it worked fine when the application ran "normally." – Rob Kennedy Jun 01 '12 at 21:50

1 Answers1

20

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);
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • I would use 'open' ...no reason to not use the open verb. ...and I do not see a difference between this and what he is currently doing. – GDF Jun 01 '12 at 20:28
  • 2
    @GDF: Read my answer. Not all applications define an `open` verb, and `open` may not be the default. Read the words I wrote. :-) Also, read the MSDN documentation on ShellExecute for yourself. (And the difference is in the second parameter to `ShellExecute`.) Like I said, if you want the **default** behavior, leave the verb empty and Windows will automatically give you the **default** behavior (the same thing you'd get by double-clicking the file in Windows Explorer). – Ken White Jun 01 '12 at 20:33
  • I see what you are saying, so if Adobe Reader does not define an open, this will be the same as a double click. However, since I know adobe reader defines an 'open', among other verbs this just seemed like an odd response to me... ...it's all good... – GDF Jun 01 '12 at 20:43
  • @GDF, the major difference is that Acrobat Reader actually opens the file this way, while the original question's way does not. :-) And I was referring to a general rule, not Reader in particular - if you want the **default** behavior, don't ask for a **specific** action. – Ken White Jun 01 '12 at 20:51
  • Thats good to know that I can just leave the verb empty and let windows decide. It works great! Thanks everyone! – Trevor Jun 01 '12 at 20:52
  • In 2018 but using Delphi 7 and XP so it matters. I also removed the 'open' and now magically works (Foxit reader). – ZioBit Dec 06 '18 at 13:28