Handling Errors in Visual Basic
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