That, to be frank, doesn't make any sense. Both ExcelApp and ExcelSheet are "object handles". Unless you specifically close the script engine entirely *or* you intentionally set them to something else, they should continue to point at the same things.
Now, what Shaun is suggesting is that your code is actually doing something like:
ExcelApp = new ActiveXObject("Excel.Application")
i = 1
function test() {
ExcelSheet = new ActiveXObject("Excel.Sheet")
ExcelSheet.Application.Visible = true;
ExcelSheet.ActiveSheet.Cells( i, 1 ).Value = "foo"
i = i + 1
}
Or even:
i = 1
function test() {
ExcelApp = new ActiveXObject("Excel.Application")
ExcelSheet = new ActiveXObject("Excel.Sheet")
ExcelSheet.Application.Visible = true;
ExcelSheet.ActiveSheet.Cells( i, 1 ).Value = "foo"
i = i + 1
}
This *won't* work. The reason is what is called "scope". Basically, in most languages a variable is only "global", which is to say accessible to all parts of your script/program if defined "before" any functions. Shaun's example does this. It creates the variable *first*, then defines a function called test(). This means that things happen this way:
main -> create ExcelApp
main -> create ExcelSheet
main -> define function test()
mushclient -> call test()
test -> Set cell information.
test -> exit
Now, if you are doing *either* of the two ways above, then what is happening is roughly this:
main -> define function test()
mushclient -> call test()
test -> create ExcelApp
test -> create ExcelSheet
test -> Set cell information.
test -> **delete** ExcelSheet
test -> **delete** ExcelApp
test -> exit
The worksheet remains "open" because Excel is also a stand alone application, so it will remain visible "unless" you specifically hide or close it. Basically, objects have "reference counts". When you open something like Excel, it does:
Excel -> Add to count, from jscript.
Excel -> Add to count, from *self*.
Excel and the worksheet you opened with it will "only" close if *both* references are released. Merely deleting ExcelSheet or even ExcelApp *only* does:
Excel -> Delete from count, jscript.
Since *self* still exists, it looks like your script is still connected to Excel, but you have in fact "lost" the connection. That is why you are having to open the new worksheet every time.
Mind you, it would be a lot easier to see what you are actually doing wrong if you posted the code itself, instead of just describing what is going wrong. ;) |