从 VB 脚本运行 Excel 宏,但不提示 VBA 运行时错误



我想从 VB 脚本中运行 Excel 宏,但没有 VBA 给出运行时错误提示,即使宏具有未捕获的运行时错误。

理想情况下,我想捕获实际的运行时错误消息,但我很高兴能够在不与对话框交互的情况下恢复脚本。

这是我所说的一个最小的例子。

test.vbs:

Option Explicit 
Dim Excel: set Excel = CreateObject("Excel.Application")
Dim wb: set wb = Excel.Workbooks.Open("C:Temptest.xls")
On Error Resume Next
Excel.run "Hello"
On Error Goto 0
wb.Close
Excel.Quit

在名为 test.xls 的 Excel 电子表格的模块 1 中:

Option Explicit
Sub Hello()
Dim x As Integer
x = 1 / 0 ' Just causing an error for this example's purposes
End Sub

关于这个问题背后的动机的简短解释。这是一项自动化任务,除了我无法更改需要自动化的宏的代码,因此我无法控制我调用的宏是否会出现未捕获的运行时错误。

Stackoverflow 上有很多很多关于使用 VB 脚本运行 Excel 宏的问题,但我找不到任何能够抑制 VBA 运行时错误提示的问题。

请在错误时包括在 子轮之后恢复

选项显式

Sub Hello()
On Error Resume Next
' Please include the error statement after sub routine.
Dim x As Integer
x = 1 / 0 ' Just causing an error for this example's purposes
End Sub

最新更新