Solved

Excel doesn't want to open

Posted on 2001-07-10
3
230 Views
Last Modified: 2010-04-06
I use this code for open Excel with parameters but until I close my Delphi(until I close the delphi application I can't open again Excel, there is no mistake ) application Excel doesn't want to open :

function TForm1.CreateProcessAndWait(const AppPath, AppParams: String;
          Visibility: word): DWord;
{-------------------------------------------------------------------------------

===============================================================================}
Const waitTime = 100; //en millisecondes ou sinon la value Infinite ... oups !
var
  SI: TStartupInfo;
  PI: TProcessInformation;
  Proc: THandle;
  state: DWord;
begin
  FillChar(SI, SizeOf(SI), 0);
  SI.cb := SizeOf(SI);
  SI.wShowWindow := Visibility;
  if not CreateProcess(PChar(AppPath), PChar(AppParams), Nil, Nil, False,
               Normal_Priority_Class, Nil, Nil, SI, PI) then
    raise Exception.CreateFmt('Impossible de creer le processus. Code Erreur %d',
                              [GetLastError]);
  Proc := PI.hProcess;
  CloseHandle(PI.hThread);
  State := STILL_ACTIVE + 1;
  repeat
    if WaitForSingleObject(Proc, waitTime) <> Wait_Failed then
               GetExitCodeProcess(Proc, State);
    Application.ProcessMessages;
  until State <> STILL_ACTIVE;
  CloseHandle(Proc);
  Result := state;
end;
0
Comment
Question by:SChatel
  • 3
3 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 50 total points
ID: 6268568
Use one of these examples:



uses Excel_tlb, ActiveX, ComObj;

var
 Excel: _Application;
 AppWasRunning: boolean; // tells you if you can close Excel when you've finished
 lcid: integer;
 Unknown: IUnknown;
 Result: HResult;
 SaveChanges: Variant;
begin
 lcid := LOCALE_USER_DEFAULT;
 AppWasRunning := False;

 {$IFDEF VER120}      // Delphi 4
 Result := GetActiveObject(CLASS_Application_, nil, Unknown);
 if (Result = MK_E_UNAVAILABLE) then
   Excel := CoApplication_.Create

 {$ELSE}              // Delphi 5
 Result := GetActiveObject(CLASS_ExcelApplication, nil, Unknown);
 if (Result = MK_E_UNAVAILABLE) then
   Excel := CoExcelApplication.Create
 {$ENDIF}

 else begin
   { make sure no other error occurred during GetActiveObject }
   OleCheck(Result);
   OleCheck(Unknown.QueryInterface(_Application, Excel));
   AppWasRunning := True;
 end;
 Excel.Visible[lcid] := True;

........

 Excel := nil;
end;






uses ComObj;

var
 Excel: Variant;
 SaveChanges: OleVariant;
begin
 try
   Excel := GetActiveOleObject('Excel.Application');    
 except
   Excel := CreateOleObject('Excel.Application');    
 end;
 Excel.Visible := True;

.........

 Excel := Unassigned;
end;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6268574
Call Excel.Quit to close Excel before Excel := nil or Excel := Unassigned.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6269967
Check this page for Excel automation:

http://www.djpate.freeserve.co.uk/AutoExcl.htm
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question