Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel doesn't want to open

Posted on 2001-07-10
3
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 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