VBS tutorial: VBScript attribute – source attribute

Time:2022-1-2

Source property

Returns or sets the name of the object or application that originally generated the error.

object.Source [= stringexpression]

parameter

object

ErrObject.

stringexpression

A string expression representing the application that generated the error.

explain

SourceProperty specifies a string expression, which is usually the class name or programming ID of the object that caused the error. Use when your code cannot handle errors caused by accessible objectsSourceProvide information to users. For example, if you access Microsoft Excel and generate aDivide by zeroError, Microsoft Excel willErr.NumberSet the error code for this error andSourceSet to excel Application。 Note that if the error occurs in another object called by Microsoft Excel, excel intercepts the error and willErr.NumberSet to its ownDivide by zeroCode for. However, it retains othersErrObjects (includingSource), as set by the object that generated the error.

SourceAlways contain the name of the object that initially generated the error. You can write code to handle the error according to the error information document of the accessed object. If the error handler fails, you can useErrObject information is user description error, useSourceAnd othersErrNotify the user of the object that initially caused the error, the error description, and so on.

When an error occurs in the code,SourceIs the programming ID of the application.

The following code illustrates how to useSourceProperties:

On Error Resume NextErr.Raise 6   ' Raise an overflow error.MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description & Err.Source)Err.Clear   ' Clear the error.