vbscript崩溃时如何关闭应用程序 [英] How to close an application when vbscript crashes

查看:109
本文介绍了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.

这篇关于vbscript崩溃时如何关闭应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆