Handling Errors in Visual Basic

3 4

When an error occurs during program execution, Microsoft Visual Basic generates an error message and halts execution. You can prevent many errors by testing assumptions before executing code that will 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 your Visual Basic documentation.

This section specifically discusses running a Microsoft Visio instance from an external program. Errors can arise from a variety of situations. For details about common situations that errors can occur in, see Handling Errors in Chapter 15, Programming Visio with VBA.

If your program requires a running Visio instance, it's a good idea to make sure the instance is there. The following Visual Basic project writes code behind the Click event for two command button controls on a Visual Basic form.

 'If you click this button, the process ID of the active 'Visio instance will be reported. You will receive a message 'that notifies you whether the GetObject function successfully 'returned an active Visio instance. Private Sub Command1_Click()       On Error Resume Next       Dim appObj As Visio.Application       Set appObj = GetObject(, "visio.application")       If appObj Is Nothing Then             MsgBox "There is no active Visio."       Else             MsgBox "ProcessID: " & appObj.ProcessID       End If End Sub 'If you click this button, a new (invisible) Visio instance 'is created and its process ID is reported. The instance is 'then made visible. You will receive a message that notifies 'you whether the CreateObject function successfully created a  'Visio instance. By creating an invisible Visio instance, the _ 'message box that contains the process ID remains visible _ 'until the user responds. Private Sub Command2_Click()       On Error Resume Next       Dim appObj As Visio.Application       Set appObj = CreateObject("visio.InvisibleApp")       If appObj Is Nothing Then             MsgBox "Failed creating Visio instance."       Else             MsgBox "ProcessID: " & appObj.ProcessID             appObj.Visible = True       End If End Sub 


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