Home Image-Pro Automation (Macros, Apps, Reports)

How to handle command errors in macros and apps

Errors in commands
Errors happening in commands usually produce exceptions rather than return some kind of error code. This is pretty typical in object oriented programming and allows to handle all possible errors globally in the code calling the command rather than having to test the return of each command.

Default Handling in macros and apps
By default, the scripting environment handles theses exceptions for the user so there is no need to add code for this purpose as long as the default behavior is appropriate for the macro or the app. Typically, a Scripting Workbench error message will appear.


Handling in macro and app code
If the default behavior is not satisfactory, which is more likely the case in apps, the code needs to handle the exception in order to implement whatever behavior is appropriate. This is done using the try/catch syntax.
    Private Sub button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles button1.Click
            Dim doc1

            With Application.DocumentCommands.Activate(Nothing)
                .Run(doc1, doc1)
            End With
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub
Sign In or Register to comment.