VBscript和Excel:打开和关闭Excel对象 - Arraylist阻止关闭对象

时间:2013-11-07 14:28:31

标签: excel vbscript arraylist

我遇到了一个vbscript问题,它创建Excel对象并从Excel文件中读取。

vbscript从Excel宏执行,然后创建并打开Excel文件并从中读取。问题是Excel对象并非总是关闭,即使我正在尝试它。

以下是vbscript的代码:

Set ExcelObject = createobject("Excel.Application")
ExcelObject.workbooks.open testWorkBookPath

Set testActionArray = CreateObject( "System.Collections.ArrayList" )

Function getTestsCaseActions (testsPath, esheet, row, col)
    Set my_sheet = ExcelObject.sheets.item(esheet)  

    tempArray = array(my_sheet.cells(row, col-2), "")
    testActionArray.Add tempArray

    Do While my_sheet.cells(row, col).Value <> ""
        tempArray = array(my_sheet.cells(row, col), my_sheet.cells(row+1, col))
        testActionArray.Add tempArray
        col = col+1
    Loop
End Function

getTestsCaseActions testWorkBookPath, testCaseSheet, 3, 4

ExcelObject.Quit

现在,如果我运行上面的代码并观察进程资源管理器,则在脚本启动时会生成一个新的Excel进程,然后按预期关闭。

但是,如果我在运行函数后插入此代码,则在ExcelObject.Quit行之前插入:

For Each ArrayItem in testActionArray
    IF ArrayItem(1) = "" Then
        Wscript.Echo ArrayItem(0)
    Else
        Wscript.Echo ArrayItem(0) & " -> " & ArrayItem(1)
    End If
Next

ExcelObject.Quit (STILL HERE)

然后生成的进程不会退出,进程列表会一直增长,直到Excel完全变成香蕉。

我不明白这一点;所有最后一段代码都是循环遍历ArrayList并打印内容。为什么这个过程没有退出?

编辑:似乎至少有一些Excel对象最终会从Process Explorer中消失,但这需要大约20-30分钟。它只是其中的一小部分 - 大多数仍在那里。目前至少我的列表缩小了一些,但仍有大约15个Excel进程在运行。

此外,突然出现此消息:

File Now Available
'filename.xlsm ' is now available for editing. Choose Read-Write to open it for editing.

1 个答案:

答案 0 :(得分:0)

这条线似乎有所帮助。它并没有完全删除所有额外的进程,但数量增加到5,然后又回到2,依此类推。所以效果很好。

dim book: for each book in ExcelObject.Workbooks: book.saved = true: next