Board index » delphi » ShellExecute and Excel XP (2002) problems

ShellExecute and Excel XP (2002) problems

The following code is supposed to open Excel.  It works fine on Windows
2000/98/NT with Excel 97/2000.  When it runs on Windows2000/XP with Excel XP
(2002) installed, Excel starts and tries to open Excel.exe as if it was a
file passed on the command line when only "/e" was passed.  I have also
tried nil for the lpParameters argument and ''.  Everytime Excel starts and
then tries to open excel.exe as if it were an excel (xls) file.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ShellAPI, StdCtrls, ComObj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    ExcelExecutable: String;
  end;

var
  Form1: TForm1;

const
  ExcelApplication = 'Excel.Application';

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  ExcelHandle: HINST;
begin
  ExcelHandle := ShellExecute(GetDesktopWindow, 'open',
                              PChar(ExcelExecutable),
                              '/e', nil, SW_SHOWMAXIMIZED);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  try
    ExcelExecutable := (GUIDToString(ProgIDToClassID(ExcelApplication)));
    ExcelExecutable := Format('CLSID\%s\LocalServer32', [ExcelExecutable]);
    ExcelExecutable := GetRegStringValue(ExcelExecutable, '');
    //Remove any switches
    if Pos('/', ExcelExecutable) > 0 then
      SetLength(ExcelExecutable, Pos('/', ExcelExecutable) - 1);
  except
    ExcelExecutable := '';
  end;
end;

end.

Thanks,

John Solgat

 

Re:ShellExecute and Excel XP (2002) problems


The following code is supposed to open Excel.  It works fine on Windows
2000/98/NT with Excel 97/2000.  When it runs on Windows2000/XP with Excel XP
(2002) installed, Excel starts and tries to open Excel.exe as if it was a
file passed on the command line when only "/e" was passed.  I have also
tried nil for the lpParameters argument and ''.  Everytime Excel starts and
then tries to open excel.exe as if it were an excel (xls) file.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ShellAPI, StdCtrls, ComObj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    ExcelExecutable: String;
  end;

var
  Form1: TForm1;

const
  ExcelApplication = 'Excel.Application';

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  ExcelHandle: HINST;
begin
  ExcelHandle := ShellExecute(GetDesktopWindow, 'open',
                              PChar(ExcelExecutable),
                              '/e', nil, SW_SHOWMAXIMIZED);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  try
    ExcelExecutable := (GUIDToString(ProgIDToClassID(ExcelApplication)));
    ExcelExecutable := Format('CLSID\%s\LocalServer32', [ExcelExecutable]);
    ExcelExecutable := GetRegStringValue(ExcelExecutable, '');
    //Remove any switches
    if Pos('/', ExcelExecutable) > 0 then
      SetLength(ExcelExecutable, Pos('/', ExcelExecutable) - 1);
  except
    ExcelExecutable := '';
  end;
end;

end.

Thanks,

John Solgat

Re:ShellExecute and Excel XP (2002) problems


<<New.borland.com:
When it runs on Windows2000/XP with Excel XP
(2002) installed, Excel starts and tries to open Excel.exe
as if it was a file passed on the command line

Quote

It's because there is a space character left at the end of
your ExcelExecutable string. If you change your code like
this:
  if Pos('/', ExcelExecutable) > 0 then
    SetLength(ExcelExecutable, Pos('/',
              ExcelExecutable) - 2);    // NB 2, not 1
it works with XP.

--
Deborah Pate (TeamB) http://delphi-jedi.org

  TeamB don't see posts sent via Google or ISPs
  Use the real Borland server: newsgroups.borland.com
  http://www.borland.com/newsgroups/genl_faqs.html

Re:ShellExecute and Excel XP (2002) problems


Thank you.  That did it.  This is very strage as the same code with
Word.Application works fine (there is a switch to remove there as well).  It
has worked with 97 and 2000 for several years.

John Solgat

"Deborah Pate (TeamB)" <d.p...@blueyonder.co.not-this-bit.uk> wrote in
message news:VA.00001272.005ca6fd@blueyonder.co.not-this-bit.uk...

Quote
> <<New.borland.com:
> When it runs on Windows2000/XP with Excel XP
> (2002) installed, Excel starts and tries to open Excel.exe
> as if it was a file passed on the command line

> It's because there is a space character left at the end of
> your ExcelExecutable string. If you change your code like
> this:
>   if Pos('/', ExcelExecutable) > 0 then
>     SetLength(ExcelExecutable, Pos('/',
>               ExcelExecutable) - 2);    // NB 2, not 1
> it works with XP.

> --
> Deborah Pate (TeamB) http://delphi-jedi.org

>   TeamB don't see posts sent via Google or ISPs
>   Use the real Borland server: newsgroups.borland.com
>   http://www.borland.com/newsgroups/genl_faqs.html

Other Threads