Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run an Excel macro from a VB Script, but don't prompt VBA runtime errors

I'd like to run an Excel macro from within a VB script, but without VBA ever giving a runtime error prompt, even if the the macro has an uncaught runtime error.

Ideally I want to capture the actual runtime error message, but I'd be happy with just being able to resume the script without interacting with a dialog.

Here's a minimal example of what I'm talking about.

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

Within module1 of an Excel spreadsheet named test.xls:

Option Explicit

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

For a short explanation as to the motivation behind this question. This is an automation task, except I can't change the code of the macro that needs to be automated, and so I can't control whether the macro I call is going to have an uncaught runtime error or not.

There are many, many questions on Stackoverflow asking about running Excel macros using VB scripts, but I haven't been able to find any that addresses being able to suppress VBA runtime error prompts.

like image 754
Hammish Avatar asked Nov 07 '22 19:11

Hammish


1 Answers

Please Inlcude On Error Resume Next after Sub rountine

Option Explicit

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
like image 130
PASUMPON V N Avatar answered Dec 05 '22 06:12

PASUMPON V N