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

标签 excel vbscript vba

我想从 VB 脚本中运行 Excel 宏,但 VBA 不会给出运行时错误提示,即使该宏存在未捕获的运行时错误。

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

这是我所讨论内容的一个最小示例。

测试.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 电子表格的 module1 中:

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

关于excel - 从 VB 脚本运行 Excel 宏,但不提示 VBA 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45273092/

相关文章:

Excel2016 : Cannot query PostgresSQL database: Server certificate not accepted

excel - 应用匹配功能如何复制粘贴数据

excel - 将儒略日期(5 或 4 个字符)转换为公历,反之亦然

Excel VBA Worksheetfunction.Match 不适用于日期

excel - 如何在 Excel 中获取显示值而不是实际值?

excel - Powershell ImportExcel 模块添加工作表问题

excel - 代号为常数?

vbscript - 当对象不可见时触发链接单击

windows - "findstr"在 VBScript 中总是返回失败

vbscript - 权限被拒绝错误 800A0046 'objIE.Document.parentWindow.screen'