Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I open any type of file with its assigned application 1

Status
Not open for further replies.

gxydas

Programmer
Jan 15, 2005
76
GR
Hi all,
I' using Delphi 7 and i have the problem below:
I use a form with a listBox and a button. The listBox lists file names and their paths. When the user clicks on the button i want to open the selected file from the listBox with its assigned application e.g. .doc with MS Word, .xls with MS Excel and so on.

Any help?
 
Here's some code that does that in one of our programs:

Code:
procedure TForm_Main.SGrid_DocsDblClick(Sender: TObject);
var
  RetVal: THandle;
  strTemp: string;
  ErrCode: integer;
  J: int64;
begin
  if LeftStr(sListPath[SGrid_Docs.Row], 5) = 'Cit #' then
    strTemp := strCitationPath + '\' + RightStr(sListPath[SGrid_Docs.Row], Length(sListPath[SGrid_Docs.Row]) - 5)
  else
    strTemp:= StringReplace(strDocPath + '/' + sListPath[SGrid_Docs.Row],
     '/', '\', [rfReplaceAll]);

  StatusBar1.SimpleText:= 'Opening ' + sListPath[SGrid_Docs.Row];
  Screen.Cursor:= crHourGlass;

  if FileExists(strTemp)
  then
    begin
      RetVal:=  ShellExecute(Handle, 'open',
        PChar(strTemp), nil, nil,
        SW_SHOWNORMAL);
    end
  else
    begin
      showmessage(strTemp +
        ' not found [procedure ListBoxDblClick]');
      Screen.Cursor:= crDefault;
      Exit;
    end;

  if RetVal > 32    // no error
  then StatusBar1.SimpleText:= 'Double click to open document'
  else
    begin
      Windows.Beep(200, 500);
      StatusBar1.SimpleText:= IntToStr(RetVal) + ' ' + strDocPath + '/' +
        sListPath[SGrid_Docs.Row];
      if RetVal = SE_ERR_NOASSOC
      then StatusBar1.SimpleText:=
        'No file type associated with ' +
        ExtractFileName(strTemp);
    end;

  Screen.Cursor:= crDefault;
end;

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual
 
Thanks a lot Leslie,
the thing i' was missing was the windows API function ShellExecute(). Also needed to add ShellAPI unit in units clause.
Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top