vbscript崩溃时如何关闭应用程序

问题描述:

我正在使用VBscript打开Microsoft Excel,并将xls文档转换为csv.
这是一个简单的示例,它带有一个参数并转换第一页

I'm using VBscript to open Microsoft Excel and convert xls documents to csv.
Here is a quick example that takes an argument and converts the first page

Dim oExcel
Dim oBook
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))

oBook.SaveAs "out.csv", 6

oBook.Close False
oExcel.Quit

如果一切正常,那就太好了.但是当脚本在无法关闭excel之前崩溃时,该过程将继续存在并锁定文件,直到我手动终止该过程为止.

If everything works, that's great. But when the script crashes before it can close excel, the process continues to stay around and lock the file until I manually kill the process.

即使脚本失败,如何确保我执行任何清理例程?

How can I make sure that I perform any clean up routines even when the script fails?

在脚本的顶部添加"On Error Goto ErrorHandler",并在其底部添加"ErrorHandler:".在ErrorHandler标签下面添加代码以根据Err.Number

Add "On Error Goto ErrorHandler" at the top of your script, and at the bottom of it, add "ErrorHandler:". Underneath the ErrorHandler label add code to manage the situation depending on the Err.Number

请参见 Err MSDN上的对象.

See Err object on MSDN.

您也可以使用"On Error Resume Next". 此处是一个示例.

You can also use "On Error Resume Next". Here is an example.

我不好. VBS中不存在转到".下面的答案可能是更整洁的方法.

My bad. "Goto" does not exist in VBS. The answer below is probably a much tidier approach.