从VB脚本运行Excelmacros,但不会提示VBA运行时错误

我想从VB脚本中运行一个Excelmacros,但是如果没有VBA提供运行时错误提示,即使这个macros有一个未被捕获的运行时错误。

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

这是我正在谈论的一个最简单的例子。

test.vbs:

Option Explicit Dim Excel: set Excel = CreateObject("Excel.Application") Dim wb: set wb = Excel.Workbooks.Open("C:\Temp\test.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 

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

有关Stackoverflow询问有关使用VB脚本运行Excelmacros的许多问题,但是我一直无法find任何能够抑制VBA运行时错误提示的地址。

请在Sub rountine之后包含错误继续

选项显式

 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