How To Use the Script Control Error Object

Article translations Article translations
Article ID: 184742 - View products that this article applies to.
This article was previously published under Q184742
Expand all | Collapse all

On This Page

SUMMARY

This article describes the Script control Error object properties and provides sample code that illustrates its use.

MORE INFORMATION

The script control Error object provides rich information to the host application regarding script errors. The following properties are supported:
   Property         Description
   -----------------------------------------------------------------
   Number           Error number.
   Description      Error message.
   Line             The line number the error occurred on (1 based).
   Column           The column number the error occurred on (0 based).
   Text*            The text of the line that caused the error.
   Source**         What component caused the error.
   HelpFile***      Help file name provided by the error source.
   HelpContext***   Help context provided by the error source.
				

* The Text property is blank for run-time errors, but contains data for compilation errors.

** The Source property for VBScript is either Microsoft VBScript compilation error or Microsoft VBScript run-time error. If a different scripting language is used, the Source may be different.

*** The HelpFile and HelpContext properties are blank except for user raised errors.

IMPORTANT: Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures.

Step-by-Step Example

  1. In Visual Basic, create a new project (Form1 is created by default).
  2. Click Components on the Project menu, and then select the "Microsoft Script Control 1.0" check box.
  3. Add the Script control (ScriptControl1), a text box (Text1), and a command button (Command1) to Form1. Set the MultiLine property of the text box to TRUE. For ease of use, change the size of Text1 so that it accommodates 5 lines with 60 character each.
  4. Add the following code to Form1:
          Private Sub Command1_Click()
            On Error Resume Next
            With ScriptControl1
              .Language = "VBScript"
              .AllowUI = True
              .AddCode Text1.Text
              .Run "Test"
            End With
            If Err Then
              MsgBox Err & " " & Error
              ListErrors ScriptControl1
            End If
          End Sub
    
          Private Sub ListErrors(S As ScriptControl)
            With S.Error
              Debug.Print "Number:", .Number
              Debug.Print "Source:", .Source
              Debug.Print "Desc:", .Description
              Debug.Print "Line: " & .Line, "Column: " & .Column
              Debug.Print "Text:", .Text
              Debug.Print "Help File:", .HelpFile
              Debug.Print "Help Context:", .HelpContext
              Debug.Print
            End With
          End Sub
    
    						
  5. Run Form1 and open the Debug window.
  6. Type the following script in the text box:
          Sub Test
            MsgBox == "Hello World"
            Err.Raise 555, "Test", "Custom Error", "test.hlp", 5
          End Sub
    
    						
  7. Click the command button. As a result, the following error appears in the Debug window (the actual output may vary depending on the version of the script control you are using):
          Number:       1002
          Source:       Microsoft VBScript compilation error
          Desc:         Syntax error
          Line: 2       Column: 16
          Text:         MsgBox == "Hello World"
          Help File:
    
          Help Context:  0
    
    						
  8. Remove the == from the MsgBox line and click the command button again.
  9. The MsgBox appears and then error "555 Custom Error". The Debug Window displays the following:
          Number:       555
          Source:       Test
          Desc:         Custom Error
          Line: 3       Column: 8
          Text:
          Help File:    test.hlp
          Help Context: 5
    
    						
    NOTE: The column number may vary depending on whether you cut-and-paste the code or type it in manually without all the leading spaces.

REFERENCES

Script control Help topic: Error object

For information about obtaining the Script control, please see the following article in the Microsoft Knowledge Base:
184739 : INFO: Where to Obtain the Script Control

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Malcolm Stewart, Microsoft Corporation

Properties

Article ID: 184742 - Last Review: July 13, 2004 - Revision: 3.1
APPLIES TO
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
  • Microsoft Visual Basic for Applications 5.0
Keywords: 
kbhowto kbscript kbvbp kbgrpdsvb KB184742

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com