Error Handling and Macros: OnError

(This post is pre-recorded. I'll follow up on any comments when I'm back).

 

Perhaps one of the weak spots of macros was error handling. The only error-related facility available was SingleStep (which, by the way, we now added an action so you can reliably turn it on and off, instead of the toggle RunCommand we currently have), which is helpful to figure out what’s going on, but that doesn't really count as error handling. It's more of a debugging tool.

 

Well, happily this has changed. There is a new macro action called OnError. As you would think, this introduces functionality that is directly related to the VBA keywords "On Error".

 

OnError has three different settings:

 

(1) 'Next', which is equivalent to "On Error GoTo Next" in VBA, and allows you to keep on running the macro while ignoring AND suppressing all errors.

 

(2) 'Fail' will revert error handling to the default behavior of having macros fail completely on hitting any errors executing actions.

 

(3) Finally, 'Macro Name' is directly related to the 'Macro name' parameter. This allows you specify a label on the macro to where the macro will jump if any errors are found. This is very similar to the "On Error GoTo <function>" in VBA. You can easily use this to put up your own error messages:

 

          OnError (Macro Name, "Error")

          OpenForm (Form, "Form1", …)

 

Error: MsgBox ("Error: Custom message", …)

By the way, in case you're wondering, if you have any action fail in the error handling actions, the macro will fail as it would in previous versions right off the bet. No recursive behavior here.

 

If you read so far, you're probably asking: this is all very nice, but can you have a global error handling macro like it is generally done in VBA? And, my friends, the answer is YES. But that's for next week. :)

 

(An interesting tid-bit: by making multiple calls to OnError you can flip the error handling going forward in the macro execution path. This gives you a lot of flexibility).