Handling Errors

3 4

When an error occurs during program execution, Microsoft Visual Basic for Applications (VBA) generates an error message and halts execution. You can prevent many errors by testing assumptions before executing code that might fail if the assumptions aren't valid. You can trap and respond to errors by using the On Error statement in your program. For details about On Error, see the Microsoft Visual Basic Help.

Errors can arise from a variety of situations. This section lists some common error situations and suggests ways of preventing them.

Running the Program in the Right Context

If you've determined the context in which your program will run, you can make some assumptions about the environment. For example, if you're writing a VBA program to handle double-click behavior, you can probably assume that a document is open and that the double-clicked shape is selected in the active window. However, there are limits to a program's ability to control user actions. For example, nothing stops a user from attempting to run a VBA program designed to handle a double-click event from the Macros dialog box instead of double-clicking the shape.

If your program requires a selected shape, check the Selection property of the active window to make sure it contains at least one object.

 Dim selectObj As Visio.Selection Set selectObj = ActiveWindow.Selection If selectObj.Count = 0 Then       MsgBox "You must select a shape first." , , "Select shape" Else       'Continue processing End If 

Verifying that Objects and Return Values Exist

It's wise to test whether a collection contains objects before attempting to access them. The following example checks to see if a document has masters before attempting to iterate through the Masters collection, which would cause an error if the collection were empty.

 If ThisDocument.Masters.Count = 0 Then       MsgBox "Document has no masters."       'Go to an error handler  End If 

If a property or method is supposed to return something, it's a good idea to make sure it actually did. For example, if your program formats a shape's text, you might want to verify that the Shape object contains text. For example:

 Dim shpObj As Visio.Shape Dim strText As String Set shpObj = ActivePage.Shapes.Items(1) strText = shpObj.Text If strText = "" Then       MsgBox "The selected shape has no text to format." , , "Format Shape Text" Else       'Continue processing End If 

Checking for Error Values

VBA has an Error function that returns a string. When an error occurs in the Microsoft Visio application, it returns an error code and a string that describes the error. Use the Error function to obtain the string associated with the error code returned by Visio. The Visio Cell object has an Error property, which indicates whether an error occurred when a cell's formula was evaluated. If your program alters ShapeSheet formulas, check this property to make sure the formula works as expected. Constants for valid error codes are declared by the Visio type library and begin with visError.



Developing Microsoft Visio Solutions 2001
Developing Microsoft Visio Solutions (Pro-Documentation)
ISBN: 0735613532
EAN: 2147483647
Year: 2004
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net