' 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 类中定义的 ItemAdded 事件过程。
' Create new instance of RefEvents class.
Dim objRefEvents As New RefEvents
' Pass file name and path of type library to this procedure.
Function AddReference(strFileName As String) As Boolean
Dim ref As Reference
On Error GoTo Error_AddReference
' Create new reference on References object variable.
Set ref = objRefEvents.evtReferences.AddFromFile(strFileName)
AddReference = True