Solved

Excel doesn't want to open

Posted on 2001-07-10
3
224 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

26 Experts available now in Live!

Get 1:1 Help Now