Excel won't terminate

K

Karl

D7, Excel 2000

After days, and weeks of dealing with this problem and posting code, I'm
still having the problem that Excel won't completely close down. (I know
this is Delphi code. But I think it's easy enough to follow that maybe
someone will have some ideas.)

There are about 120 workbooks in this loop which are opened and closed
thusly:


// enter 'While' loop

while (there are workbooks to open)

// opens in read only
WBk := FExcel.Workbooks.Open( FileName, False, True,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, LCID);

// read values from 5 named ranges

// then......
WBk.Close( False, EmptyParam, EmptyParam, LCID );
WBk := nil;

end; // exit while loop

// finally
FExcel.Quit;
FExcel := nil; <------- Excel is still listed as a task after this is
executed. How come?


My question is, does anyone have any suggestions as to how I might debug
this problem?

TIA,
Karl


////////////////////

procedure TdmPortMan.GetModelData;
var
LCID: integer;
WBk: _Workbook;
FExcel: _Application;
I: Integer;
FileName: String;
ModelData: Variant;
begin
FExcel := CoExcelApplication.Create;
LCID := GetUserDefaultLCID;
adoTBLSecurityMaster.First;

while NOT( adoTBLSecurityMaster.Eof ) do begin

if (adoTBLSecurityMaster.FieldByName( 'ModelWorkbook' ).Value = Null )
then begin
adoTBLSecurityMaster.Next;
Continue;
end;

FileName := FExcelWorkingDirectory +
adoTBLSecurityMaster.FieldByName( 'ModelWorkbook' ).Value + '.xls';
if ( NOT( FileExists( FileName ) ) ) OR
( adoTBLSecurityMaster.FieldByName( 'Status' ).Value =
KInactiveStatus ) then begin
adoTBLSecurityMaster.Next;
Continue;
end;

// opens in read only
WBk := FExcel.Workbooks.Open( FileName, False, True,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, LCID);

adoTBLSecurityMaster.Edit;

try
ModelData := WBk.Names.Item( {'pm'} EmptyParam, 'pm' {EmptyParam},
EmptyParam).RefersToRange.Value;
except
end;

if NOT( VarIsError( ModelData ) ) then
adoTBLSecurityMaster.FieldByName('pm').Value := ModelData;
ModelData := '';

{...similar constructs as above to read 5 more name ranges...}

WBk.Close( False, EmptyParam, EmptyParam, LCID );
WBk := nil;
Application.ProcessMessages;
adoTBLSecurityMaster.Post;
adoTBLSecurityMaster.Next;
end; // while
FExcel.Quit;
FExcel := nil;
end;
 
A

AZ

Is Excel anytime terminating??

I have similar problem when trying to call Excel from Acces VBA. I've reduced my program to such code

Dim ExcelApp As Excel.Applicatio
Set ExcelApp = CreateObject("Excel.Application"
ExcelApp.Qui
Set ExcelApp = Nothin

And the Excel does NOT terminate - it is still in Task Manager. It terminates only when I close Access file
When I set Visible then Quit cause to close Excel window but it remains in Task Manager
I'm looking for the anser What's going on, too

P
I've read a post about Excel.ApplicationClass - but I can't see ApplicationClass in VBA
I use Office XP on Windows XP
 
T

Taras Pich

This is vb.net code but try something like this after quitting the
application and before set to nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp)

AZ said:
Is Excel anytime terminating???

I have similar problem when trying to call Excel from Acces VBA. I've
reduced my program to such code:
Dim ExcelApp As Excel.Application
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Quit
Set ExcelApp = Nothing

And the Excel does NOT terminate - it is still in Task Manager. It
terminates only when I close Access file.
 
C

cgr

Releasing every Excel object has worked for me:

Dim objExcelApp As Excel
Dim objWorkbook As Workbook
Dim objWorksheet As Worksheet
Dim objRange1 As objRange1
Dim objRange2 As objRange2
..
..
..
Set objRange1 = Nothing
Set objRange2 = Nothing
Set objWorksheet = Nothing

objWorkbook.Close SaveChanges:=False
Set objWorkbook = Nothing

objExcel.Quit
Set objExcel = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top