Click to See Complete Forum and Search --> : Closing an Excel COM connection???


mneron
04-17-2003, 03:10 PM
Here is my problem :

I try to close a COM connection, but unfortunatly I can't...
I always have to open the task manager to close it!

$excel = new COM("Excel.application");

#Open the workbook that we want to use.
$wkb = $excel->Application->Workbooks->Open("$filename");
$sheets = $wkb->Worksheets($sheet);
$sheets->Activate;

#Ok We get the Vertical Limit How many Column do we have?
$VLimit = $sheets->VPageBreaks(1);
$vLimit = $VLimit->Location->Column;

#Ok We get the Horizontal Limit How many Row do we have?
$HLimit = $sheets->HPageBreaks(1);
$hLimit = $HLimit->Location->Row;

print "#Column: $vLimit <br>#Row: $hLimit";

# Close the EXCEL File!
$excel->Application->ActiveWorkbook->close("False");
$excel->Quit();
$excel->Release();
unset($excel);

exit();


It seems that the part where I call the function to close it is not working, anyone with clues can help me please!?!?!.

shareaweb
04-20-2003, 05:05 PM
When first writing applications instancing COM objects I always faced the problem of the instanced program not terminating correctly although everything else worked fine. In fact the process had to be killed manually by using the task manager. When experimenting a bit I was able to isolate the cause for this peculiar behaviour to be illustrated by the sample code below:
<?php
//Accessing arrays by retrieving elements via function ArrayName(Index) works, but certainly is neither efficient nor good style
$excel=new COM("Excel.Application");
$excel->sheetsinnewworkbook=1;
$excel->Workbooks->Add();
$book=$excel->Workbooks(1);
$sheet=$book->Worksheets(1);
$sheet->Name="Debug-Test";
$book->saveas("C:\\debug.xls");
$book->Close(false);
unset($sheet);
unset($book);
$excel->Workbooks->Close();
$excel->Quit();
unset($excel);
//Accessing arrays as usual (using the [] operator) works, buts leads to the application not being able to terminate by itself
$excel=new COM("Excel.Application");
$excel->sheetsinnewworkbook=1;
$excel->Workbooks->Add();
$excel->Workbooks[1]->Worksheets[1]->Name="Debug-Test";
$excel->Workbooks[1]->saveas("C:\\debug.xls");
$excel->Workbooks[1]->Close(false);
$excel->Workbooks->Close();
$excel->Quit();
unset($excel);
?>
The sample code performs the same action twice and each time the file is properly created. Yet for some mysterious reason the instanced excel process won't terminate once you've accessed an array the way most programmers (especially those who do a lot of oop in c++) do! Therefore until the PHP developers become aware of this problem we'll have to use the inefficient coding style illustrated in the first example.

mneron
04-21-2003, 12:49 PM
Thank you it really help me out !