从项目中移除引用时发生 ItemRemoved 事件。
语法
表达式。ItemRemoved (参考)
表达式 表示 引用 对象的变量。
参数
| 名称 | 必需/可选 | 数据类型 | 说明 |
|---|---|---|---|
| Reference | 必需 | Reference | 从项目中删除的引用。 |
备注
ItemRemoved 事件适用于 References 集合。 它不与控件、窗体或报表关联,这与大多数其他事件一样。 因此,若要为 ItemRemoved 事件过程创建过程定义,必须使用特殊的语法。
ItemRemoved 事件只能在发生时运行事件过程;它无法运行宏。
该事件仅在从代码删除引用时发生。 从“参考”(References) 对话框中删除引用时,不会发生此错误,当“模块”(Module) 窗口为活动窗口时,可通过选择“工具”(Tools) 菜单上的“参考”(References) 来获得。
示例
下面的示例包含 ItemAdded 和 ItemRemoved 事件的事件过程。 若要尝试此示例,请先在“插入”菜单上选择“类模块”来创建一个新的类模块。 将以下代码粘贴到类模块中,并将模块另存为 RefEvents。
' Declare object variable to represent References collection.
Public WithEvents evtReferences As References
' When instance of class is created, initialize evtReferences
' variable.
Private Sub Class_Initialize()
Set evtReferences = Application.References
End Sub
' When instance is removed, set evtReferences to Nothing.
Private Sub Class_Terminate()
Set evtReferences = Nothing
End Sub
' Display message when reference is added.
Private Sub evtReferences_ItemAdded(ByVal Reference As _
Access.Reference)
MsgBox "Reference to " & Reference.Name & " added."
End Sub
' Display message when reference is removed.
Private Sub evtReferences_ItemRemoved(ByVal Reference As _
Access.Reference)
MsgBox "Reference to " & Reference.Name & " removed."
End Sub
以下 Function 过程删除指定的引用。 删除引用时,RefEvents 类中定义的 ItemRemoved 事件过程运行。
例如,若要删除对日历控件的引用,可以传递"MSACAL",是表示日历控件的 引用 对象的名称的字符串。
Function RemoveReference(strRefName As String) As Boolean
Dim ref As Reference
On Error GoTo Error_RemoveReference
' Return object representing existing reference.
Set ref = objRefEvents.evtReferences(strRefName)
' Remove reference from collection.
objRefEvents.evtReferences.Remove ref
RemoveReference = True
Exit_RemoveReference:
Exit Function
Error_RemoveReference:
MsgBox Err & ": " & Err.Description
RemoveReference = False
Resume Exit_RemoveReference
End Function
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。