CorelDraw & VBA
Back to the Main Menu
VBA Introduction
Artistic Text
Paragraph Text
Finding & Changing Text
Master Layers
Opening Other Applications
User Interface
Event Programming
Storing User Data
Error Handling
Speeding Up CorelDraw
VBA Security
Guide to CorelDraw VBA

Error Handling

Error Handling is placing in your code provision for expected and unexpected errors. Expected errors are simply those that you have anticipated. However there can be many errors that you have not thought of. What if the user inputs a letter instead of a number. Maybe the code refers to a file has since been renamed. What if the document does not contain a particular object that is to be colored. It is difficult to predict all possible causes of errors.

Error handling is not that important when you are the only user. You can read the code and so you can determine what went wrong. However, if others are to use your code and they are not familiar with VBA then you should incorporate Error Handling in your code. If these people see a dialog box informing them of an error they will not necessarily trust your code since they can perceive that it has errors. Also if they are confronted with a dialog box giving them the choice of ending the code or debugging the code and they choose debugging then they will get into areas where they are over their heads and may inadvertently change your code.

The best source to explain the use of Error handlers in at the Pearson Software Consulting site. It apply's to both CorelDraw and Excel as well as to all other applications running VBA.

The simplest error handler is to place the follow at the start of the code just after "Sub .....()", "Function .....()" or "Property .....()".

    On Error GoTo Err_Handler

At the end of the code just before the "End Sub", "End Function" or "End Property" place the following.

    Exit Sub

    MsgBox "An unexpected error has occurred" & vbCr & "This code will cease."

The above means that if there is an error anywhere in this code immediately go to the line called Err_Handler and by-pass the rest of the code. Then it perform all the commands following the line Err_Handler. In this case a message box will displayed informing the user of an error and that the code will cease. The final line of code "End Sub", "End Function" or "End Property" will stop the code from running further.

Notice that the line above the Err_Handler code is "Exit Sub", "Exit Function" or "Exit Property". If there were no errors the code would be performed and would stop when it reached this Exit otherwise the Err_Handler code would also run even though there had been no errors.

Here is a sample bas file that you can import into your VBA code to see how it works. Run Sub First_Procedure(). It will display a message box with the answer of 18 divided by 2 then it will automatically run Sub Second_Procedure() that tries to divide by zero. Sub Second_Procedure() does not contain an Error Handler but the last Error Handler in Sub First_Procedure() will take over and stop the code.

Issued 2017_08_31