开发者

Correct code for opening a file with an external editor

Is the following code it right or not and, if it is wrong, please correct it.

Note: I want to open the file with "WordPad.exe" not with "Microsoft Office Word" until if "Microsoft Office Word" is the default program.

My code:

function InitializeSetup: Boolean;
 var
   S: AnsiString;
 begin
   // Show the contents of Readme.txt (non Unicode) in a message box
   ExtractTemporaryFile('Info.rtf');
   Result := True;
 end;

 procedure AboutButtonO开发者_如何学CnClick(Sender: TObject);
 var
   ErrorCode: Integer;
 begin
  ShellExec('open', ExpandConstant('{tmp}\Info.rtf'), '', '', SW_SHOWNORMAL, ewNoWait,
 ErrorCode);
 end;


ShellExec('open','Documentname'....); will open with the program associated with the extension of the file. If there is no program associated it will prompt you to select which program you want to view it with.

You could look for WordPad.exe and if it's found you could call ShellExec using the WordPad.EXE directly. Then pass the documentName as a parameter.

Updated with function to do this

procedure OpenDocumentInWordPad(Document : String);
var
 WordPad : String;   
 ErrorCode : Integer;
begin
   // Typical Location on XP and later.
   WordPad := ExpandConstant('{pf}') + '\Windows NT\Accessories\WordPad.exe'
   // Find word pad
   if Not FileExists(WordPad) then
   begin
     // Location in Windows 95/98
     WordPad := ExpandConstant('{pf}') + '\Accessories\WordPad.exe'
     if Not FileExists(WordPad) then
     begin
       // Fall back to anything associated with document.
       WordPad := Document;
       Document := '';
     end;
   end;

   if not ShellExec('open',WordPad,Document,'',SW_SHOW,ewNoWait,ErrorCode) then
   begin
      MsgBox(SysErrorMessage(ErrorCode),mbError,MB_OK);
   end;
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜