On Wednesday, February 14, 2018 at 8:51:19 PM UTC+5:30, Nikh wrote:
> I had it with and without '$application Quit' command but same behavior. I was finally able to figure this out. I have t different 'range' in the code. If I do 'range -destroy' before I invoke the subsequent 'range' it clears the excel.exe running in the background. So if I have 3 range's I'll have 3 range - destroy commands. I was using tcom before (32bit) and I only need to do $application Quit. I recently switched to twapi package. May be I'll consider cawt package going forward.
Yes, unlike tcom which binds object lifetimes to references, twapi's com interface is layered on TclOO and requires explicit destroying. This can be mitigated to some extent using the "-with" option to reduce the number of temporary objects that need to be destroyed. For example, your script could be written as
package require twapi
set excelOutFile {c:\temp\aaa.xlsx}
set application [twapi::comobj Excel.Application]
set workbook [$application -with Workbooks Add]
set worksheet [$workbook -with Worksheets Item 1]
$worksheet Activate
$worksheet -with {{Range "A1"}} ColumnWidth 20.0
$worksheet -with {{Range "A1" "A8"}} NumberFormat 0.0
set a "88501200600667"
$worksheet -with Cells Item 1 1 $a
$workbook SaveAs $excelOutFile
$worksheet -destroy
$workbook -destroy
$application Quit
$application -destroy
See
http://www.magicsplat.com/book/com.html for more details if you haven't already.
As an aside, note that CAWT is also layered on twapi but much easier to use to interface to the Office API.
/Ashok