前のトピック     次のトピック

第 26 章 MICRO SOFT VISUAL BASIC による VISIO アプリケーションのプログラミング

セクション 3   Microsoft Visual Basic のエラー処理

プログラムの実行中にエラーが発生すると、Microsoft Visual Basic ではエラー メッセージが生成され、実行が中止されます。予想外の操作によってコードの実行に失敗することがないように、コードを実行する前にあらゆる処理のパターンをテストします。そうすると、多くのエラーは未然に防ぐことができます。プログラム内で On Error ステートメントを使用すると、エラーをトラップし、エラー処理を行うことができます。On Error ステートメントの詳細については、Visual Basic のマニュアルを参照してください。

このセクションでは、外部プログラムから Visio® インスタンスを実行する方法について具体的に説明します。エラーはさまざまな状況で発生します。エラーが発生する一般的な状況の詳細については、「第 15 章 Visio での Microsoft VBA プログラミング」の「エラー処理」を参照してください。

作成するプログラムが、実行中の Visio インスタンスを必要とする場合は、インスタンスの状態を確認することをお勧めします。以下の Visual Basic プロジェクトでは、Visual Basic フォーム上の 2 つのコマンド ボタンに対して Click イベントを引き起こすコードが記述されています。

'If you click this button, the processID 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 "アクティブな Visio インスタンスはありません."
     Else
          MsgBox "プロセス ID: " & appObj.ProcessID
     End If

End Sub

'If you click this button a new (invisible) Visio instance is created and
'its processID 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 Msgbox
'containing the processID 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 "Visio インスタンスの作成に失敗しました."
     Else
          MsgBox "プロセス ID: " & appObj.ProcessID
          appObj.Visible = True
     End If
End Sub

Top