On Error 语句中的错误 [英] Error in On Error statement

查看:24
本文介绍了On Error 语句中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全不是 VBScript 开发人员.但是通常情况下,我必须编写一个小脚本来检查某些内容.它打开 Excel,向其中写入内容并关闭它.但这不是重点.关键是我无法编写用于错误处理的代码.这个脚本:

I am totally not a VBScript developer. But as it usually happens I have to write a small script to check something. It opens Excel, writes something to it and closes it. But that's not the point. The point is that I cannot manage to write code for error handling. This script:

Sub Work()
On Error GoTo ErrMyErrorHandler
Dim objExcelApp
Dim wb
Dim ws

Set objExcelApp = CreateObject("Excel.Application")
Set wb = objExcelApp.Workbooks.Add(True)
Set ws = wb.Sheets(1)

ws.Cells(1,1).Value = "Hello"
ws.Cells(1,2).Value = "World"

wb.SaveAs("c:\test.xls")
objExcelApp.Quit()
Exit Sub

ErrMyErrorHandler:
MsgBox Err.Description, vbExclamation + vbOKCancel, "Error: " & CStr(Err.Number)    
End Sub

Work()

给出这个错误:

第 2 行是带有 On Error 语句的行.我做错了什么?

Line 2 is the line with the On Error statement. What am I doing wrong?

谢谢.

推荐答案

看起来您无法在 VB 脚本中将自定义标签指向错误处理程序.您只能使用

looks like you can not point custom label to error handler in VB Script. You can only use

on error goto 0 '(raises exceptions)
on error resume next '(ignores exceptions)

如果你使用第二种语法,你可以通过 Err 全局变量捕获发生的异常:

if you use the second syntax, you can catch occurring exceptions via Err global variable:

  if Err.Number <> 0 then MsgBox "Exception occurred: " & Err.Description

这篇关于On Error 语句中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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