vbscript 崩溃时如何关闭应用程序 [英] How to close an application when vbscript crashes
问题描述
我正在使用 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
标签下添加代码以根据 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
参见 错误
MSDN 上的对象.
See Err
object on MSDN.
您也可以使用On Error Resume Next".这里是一个例子.
You can also use "On Error Resume Next". Here is an example.
我的错.VBS 中不存在Goto".下面的答案可能是一种更整洁的方法.
My bad. "Goto" does not exist in VBS. The answer below is probably a much tidier approach.
这篇关于vbscript 崩溃时如何关闭应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!