0

In the past, I use the following statement to open a file:

        if ShellExecute(Handle, 'open', PChar(txtFile.Text), nil, nil, SW_SHOWNORMAL) <= 32 then
          MessageDlg('Error', mtError, [mbOK], 0);

However, in Windows 11, when I try to open a .pptx file, the statement fails and shows the error. ShellExecute will return 31.

I try to double-click to open the .pptx file directly, and I see the following dialog: enter image description here

So, I hope my code works just like double-click the file in Windows, i.e. show the above "choose app" dialog instead of showing error.

I follow the instructions in How can you open a file with the program associated with its file extension? and change my code to:

        if ShellExecute(Handle, nil, PChar(txtFile.Text), nil, nil, SW_SHOWNORMAL) <= 32 then
          MessageDlg('Error', mtError, [mbOK], 0);

But still not work. ShellExecute will return 31.

I try to use 'openas' instead as mentioned in How to open a default dialog for window if ShellExecute fails due to no file association in C++?, as follows:

ShellExecute(Handle, 'openas', PChar(txtFile.Text), nil, nil, SW_SHOWNORMAL);

But still fail and ShellExecute still returns 31.

alancc
  • 487
  • 2
  • 24
  • 68
  • Have you tried the `'openas'` verb yet? – Remy Lebeau Oct 05 '22 at 23:52
  • Is `txtFile.Text` a fully qualified file name? – Andreas Rejbrand Oct 06 '22 at 07:57
  • @AndreasRejbrand, yes, it is. If the file is associated with an app, then "open" and nil will work, but "openas" will still return 31. I think "openas" is not supported by all operating systems, I am using Win 7. – alancc Oct 06 '22 at 08:32
  • @AndreasRejbrand, but even in the latest Win 11, "openas" still not working. – alancc Oct 06 '22 at 22:11
  • @alancc: I just tried it on Windows 7. With an unknown extension, (1) no verb specified and (2) `openas` both display the unknown file type dialog box, which lets you open the Open With dialog. `open` makes the function call a no-op. With a known extension, I am unable to display the Open With dialog at all (in particular, `openas` doesn't do it). – Andreas Rejbrand Oct 06 '22 at 22:39
  • @AndreasRejbrand, Thank you for your test. I retest on Win 7 and confirm you are correct. nil & openas will work on Win 7. open will return 31. – alancc Oct 07 '22 at 07:20
  • @AndreasRejbrand, sorry for my overlook on this issue. I find actually the issue only occurs with some special files. When double-clicking such files, a special "Open with" dialog which includes one app will show. In such a case, ShellExecute on such files will fail for all open, openas and nil. I have created a new question https://stackoverflow.com/questions/73983830/how-to-show-open-with-dialog-with-one-app-included – alancc Oct 07 '22 at 07:36

0 Answers0