Microsoft KB Archive/81364

{|
 * width="100%"|

WD: Trapping WordBasic ERR=102 After Choosing Cancel Button

 * }

Q81364

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows, versions 7.0, 7.0a
 * Microsoft Word for the Macintosh, versions 6.0, 6.0.1

-

SUMMARY
In Microsoft Word, when you choose the Cancel button in a custom dialog box, you receive the following error message:

WordBASIC err=102

Command Failed

To Word, the Cancel button indicates a run-time error; therefore, Word halts execution of the macro. You can trap this error with an On Error command in your macro if you want to suppress the message and let the macro continue.

An alternative method of trapping this error is to call the function form of the dialog box. When you choose the Cancel button, a value of 0 (zero) is returned instead of a WordBasic error.

MORE INFORMATION
An error trap statement within a sample section of macro code resembles the following example:

  Sub Main On Error Goto ExitMacroDialog Begin Dialog UserDialog '  '   End Dialog '  '   ExitMacroDialog: '  '   End Sub where &quot;ExitMacroDialog&quot; is a label somewhere beyond the dialog statement for the dialog box.

The lines that include an apostrophe indicate there may be more macro code between the given statements. If you receive the WordBasic error 102 after putting the error trap into the macro, check to make sure the label is valid and that the On Error statement appears in order before the Dialog statement.

Alternative Error Trapping Method 1:
The alternative method for trapping the Cancel button in a custom dialog box is to call the function form of a dialog box. If you trap an error with this method, the &quot;On Error Goto&quot; statement is not used. The following is a sample macro that creates a custom dialog box using the function form of the dialog box. This method returns a value of 0 (zero) if you choose the Cancel button instead of halting the macro and generating the WordBasic error 102.

  Sub Main Begin Dialog UserDialog 320, 124, &quot;Word for Windows 2.0&quot; TextBox 12, 30, 160, 18, .TextBox1 TextBox 13, 83, 160, 18, .TextBox2 Text 18, 10, 92, 13, &quot;First Name :&quot; Text 17, 60, 92, 13, &quot;Last Name :&quot; OKButton 205, 41, 88, 21 CancelButton 205, 65, 88, 21 End Dialog Dim dlg As UserDialog '  Select Case Dialog(dlg) Case - 1 MsgBox &quot;Rest of Macro...&quot; Case Else MsgBox &quot;Exit Macro. Choose OK Button.&quot; End Select '  RestofMacro: MsgBox &quot;Rest of Macro...&quot; End Sub

Alternative Error Trapping Method 2:
This method also avoids the use of the &quot;On Error Goto&quot; statement. The &quot;If&quot; statement will run only if the OK button is clicked.

Sub Main Begin Dialog UserDialog 320, 124, &quot;Word for Windows 2.0&quot; TextBox 12, 30, 160, 18, .TextBox1 TextBox 13, 83, 160, 18, .TextBox2 Text 18, 10, 92, 13, &quot;First Name :&quot; Text 17, 60, 92, 13, &quot;Last Name :&quot; OKButton 205, 41, 88, 21 CancelButton 205, 65, 88, 21 End Dialog Dim dlg As UserDialog

If Dialog(dlg) then MsgBox &quot;Rest of Macro[ASCII 133]&quot; End If End Sub