Solved

Excel doesn't want to open

Posted on 2001-07-10
3
226 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now