p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   Javascript & ActiveX Excel Object Process (http://p2p.wrox.com/showthread.php?t=53738)

interrupt February 7th, 2007 06:46 AM

Javascript & ActiveX Excel Object Process
Hi All,

I might be being a bit retarded here, but I'm having a bit of a silly issue and for some reason I can't wrap my brain around it. I've built a routine that exports the contents of a table into Microsoft Excel, through the use of Javascript and ActiveX. Here's the code:

<script language = "javascript">
var iCellsStr = ""
var hCellsStr = ""
var rangeStr = "ABCDEF"
var rowCount = 1
var colCount = 1
function xcTable()
var eWApp = new ActiveXObject("Excel.Application")
eWApp.Visible = true
xlSheet = eWApp.ActiveSheet

for (iCells = 0; iCells < t1.cells.length; iCells++)

iCellsStr+=(t1.cells[iCells].innerText + " ")
cellWriteStr = rangeStr.substring(rowCount-1, rowCount) +colCount
xlSheet.Range(cellWriteStr).Value = t1.cells[iCells].innerText

if(rowCount ==7)
rowCount = 1

rowCount = 1
colCount = 1

eWApp = null;

What I'd like to be able to do is leave the Excel book open until the user has finished with it (they will close it at their leisure). However, if I do that, there is a process left in the process tree that doesn't get killed when the user exits Excel. If I do the closure from Javascript through the workbook object first, however (following), it does kill the process.

Instead of having:

I can have:
var eWkBookAdd = eWApp.Workbooks.Add()

This allows me to close the object with:

But if my user wants to work with the data straight away I can't kill the process! I tried setting the eWApp object to null, hoping that when the user was finished with Excel, I could use the GarbageCollect() method, but this didn't work either. In fact it caused a Doctor Watson error in Excel and crashed it. Is there any workaround for this or even a sensible suggestion? Or am I trying to open pandora's box?

Cheers in advance

'sync' <cr>
The name specified is not recognized as an internal or external command, operable program or batch file.

joefawcett February 7th, 2007 08:30 AM

You can try setting eWAPP.UserControl = true at some stage. Personally I dislike using ActiveSheet and similar in automation, it's too likely to go wrong. I would use:

var oWorkbook = eWApp.Workbooks.Add()
xlSheet = oWorkbook.Sheets[0] //or oWorkbook.Sheets[1] I can't remember if Sheets is one or zero based.


Joe (Microsoft MVP - XML)

All times are GMT -4. The time now is 04:00 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.