共用方式為


Access (References.ItemAdded 事件)

從 Visual Basic 新增參照至專案中時,會發生 ItemAdded

語法

運算式ItemAdded (參考)

表達 代表 References 物件的 變數。

參數

名稱 必要/選用 資料類型 描述
Reference 必要 Reference 新增至專案中的參照

註解

ItemAdded事件會套用至References集合。 它與控制項、表單或報表沒有關聯,就像大多數其他事件一樣。 因此,若要建立 ItemAdded 事件程序的程式定義,您必須使用特殊的語法。

ItemAdded事件在發生時只能執行事件程序;它無法執行宏。

只有從程式碼新增參照時,才會發生此事件。 當您從 [參考] 對話方塊新增參時,當 [模組] 視窗是使用中視窗時,請選擇 [工具] 功能表上的 [參考],就不會發生此情況。

範例

下列範例中包含 ItemAddedItemRemoved 事件的事件程序。 若要嘗試此範例,請先選擇 [插入] 功能表上的 [類別模組],以建立新的類別模組。 將下列程式碼貼入類別模組,並將模組儲存為 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

下列 函式 程式會新增指定的參考。 新增參考時,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 
 
Exit_AddReference: 
 Exit Function 
 
Error_AddReference: 
 MsgBox Err & ": " & Err.Description 
 AddReference = False 
 Resume Exit_AddReference 
End Function

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應