-1

As far as i know ... You need to set the executable program in ShellExecute (like notepad.exe) but what if the user wants to use other external program (like notepad++.exe) to open the file? if this is possible then how will i do that?

Rhacel
  • 1
  • 1

1 Answers1

1

My guess is that what you really want to do is open a document using whatever program the user has chosen to associate with that document type. In which case you pass the document to ShellExecute rather than the program:

ShellExecute(0, nil, PChar(DocumentFileName), nil, nil, SW_SHOWNORMAL);

Passing nil for the second parameter, the verb, uses the default verb for the document. Usually this will be the 'open' verb. But not always. So you might instead write:

ShellExecute(0, 'open', PChar(DocumentFileName), nil, nil, SW_SHOWNORMAL);

If you want to be able to check for errors in a sane way you will need to use ShellExecuteEx rather than ShellExecute.


Your comments shed a little more light on the question, although I'm still clutching at straws. You seem to want to be able to open an RTF file in the user's preferred text editor. Do that using ShellExecuteEx passing a class name that instructs the shell to treat the file as if it were text.

var
  sei: TShellExecuteInfo;
....
ZeroMemory(@sei, SizeOf(sei));
sei.cbSize := SizeOf(sei);
sei.fMask := SEE_MASK_CLASSNAME;
sei.lpVerb := 'open';
sei.lpFile := PChar(DocumentFileName);
sei.nShow := SW_SHOWNORMAL;
sei.lpClass := '.txt';
Win32Check(ShellExecuteEx(@sei));
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Yes you are right, i want to open a document. im creating this kind of program where their is RichEdit and two buttons (1st button: Refresh, 2nd button: Open As). when i click on the 1st button a data or file will show in the RichEdit and when i click on the 2nd button the user will specify what kind of editor or executable file will he/she wants to use in order to view the data/file shown in the RichEdit.. but i dont have an idea on how to do this – Rhacel Oct 21 '14 at 13:41
  • You need to take some decisions before you go any further. You aren't ready to start coding yet. You need to decide what your program is going to do. How is the user going to express their preferences? Perhaps you want them to open .rtf as text? In which case that's easy to do with `ShellExecuteEx`. Is that really what you want> – David Heffernan Oct 21 '14 at 13:44