To create an event procedure that runs when the ItemAdded event occurs, follow the steps included in the Remarks section of this topic.
Syntax
Private Sub evtReferences_ItemAdded(reference)
The ItemAdded 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 added to the References collection. |
Remarks
The ItemAdded event procedure can contain code that you want to run when a reference to a type library is added to the project. You can add a reference from Visual Basic by using the AddFromFile or AddFromGUID method to add a Reference object to the References collection.
To create the ItemAdded event procedure:
Public WithEvents evtReferences As References
evtReferences
in the Object drop-down box. Then select the ItemAdded 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 ItemRemoved event, you don't need to repeat them for the ItemAdded event. You can use the same References object variable for both.
In order to trigger the ItemAdded event, you must create the new reference by using the References object variable that you declared with the WithEvents keyword.
To add a reference and trigger the event:
Dim objRefEvents As New RefEvents
evtReferences
). Since the ItemAdded 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 add a new reference with a procedure such as the following:
Function AddReference(strFileName 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.AddFromFile(strFileName)
End Function
Notes