Source 属性

Returns or sets a string expression specifying the name of the object or application that originally generated the error. 读/写。

备注

Source 属性指定一个字符串表达式,表示生成错误的 对象;表达式通常是对象的名或编程 ID。

Use Source to provide information when your code is unable to handle an error generated in an accessed object. 例如,如果访问 Microsoft Excel 并生成 Division by zero 错误,Excel 会将 Err.Number 设置为该错误的错误代码,并将 “源” 设置为 Excel.Application

从代码生成错误时,“源”是应用程序的编程 ID。 对于类模块,“源”应包含具有形式 project.class 的名称。

代码中出现意外错误时,将自动填充“源”属性。 对于标准模块中的错误,“源”包含项目名称。 对于类模块中的错误,“源”包含具有project.class形式的名称。

示例

本示例将 Visual Basic 中创建的自动化对象的编程 ID 分配给变量 MyObjectID,然后在 Err 对象的 Source 属性生成 Raise 方法错误时将其分配给该对象的 Source 属性。

处理错误时,不应以编程方式使用 Source 属性 (或任何数字) 以外的 Err 属性。 The only valid use of properties other than Number is for displaying rich information to an end user in cases where you can't handle an error. The example assumes that App and MyClass are valid references.

Dim MyClass, MyObjectID, MyHelpFile, MyHelpContext
' An object of type MyClass generates an error and fills all Err object
' properties, including Source, which receives MyObjectID, which is a 
' combination of the Title property of the App object and the Name
' property of the MyClass object.
MyObjectID = App.Title & "." & MyClass.Name
Err.Raise Number := vbObjectError + 894, Source := MyObjectID, _
          Description := "Was not able to complete your task", _
          HelpFile := MyHelpFile, HelpContext := MyHelpContext 

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。