Solved

Excel doesn't want to open

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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