To create an event procedure that runs when the ItemRemoved event occurs, follow the steps included in the Remarks section of this topic.
Syntax
Private Sub evtReferences_ItemRemoved(reference)
The ItemRemoved event procedure has the following arguments.
Argument | Description |
---|---|
evtReferences | An object variable representing the References collection. You must declare this object variable in a class module by using the WithEvents keyword. |
reference | The Reference object that has been removed from the References collection. |
Remarks
The ItemRemoved event procedure can contain code that you want to run when a reference to a type library is removed from the project. You can remove a reference from Visual Basic by using the Remove method to remove a Reference object from the References collection.
To create the ItemRemoved event procedure:
Public WithEvents evtReferences As References
evtReferences
in the Object drop-down box. Then select the ItemRemoved event in the Procedure box.For example, you could include this statement within the Initialize event of the class module in which you declared the object variable. Then when you create an instance of the class, the object variable is automatically initialized. If you're using the evtReferences
object variable, this event procedure would appear as follows:
Private Sub Class_Initialize()
Set evtReferences = Application.References
End Sub
Note If you've already followed the preceding steps to create an event procedure for the ItemAdded event, you don't need to repeat them for the ItemRemoved event. You can use the same References object variable for both.
In order to trigger the ItemRemoved event, you must remove the existing reference by using the References object variable that you declared with the WithEvents keyword.
To remove a reference and trigger the event:
Dim objRefEvents As New RefEvents
evtReferences
). Since the ItemRemoved event in the class module is defined in association with a particular References object variable, you must create the new Reference object by using this variable. Otherwise, the event won't occur.Note that you must qualify the References object variable with the name of the class in which it's declared.
Once you've completed all the preceding steps, you can remove an existing reference with a procedure such as the following:
Function RemoveReference(strRefName As String)
Dim ref As Reference
' Create new instance of RefEvents class.
Dim objRefEvents As New RefEvents
' Creates reference on References collection
' variable defined in RefEvents class.
Set ref = objRefEvents.evtReferences(strRefName)
objRefEvents.evtReferences.Remove ref
End Function
Notes