共用方式為


列出屬於 Exchange 通訊群組清單之每個管理員的名稱和辦公室位置

本主題說明如何允許使用者選取 Exchange 通訊群組清單,並顯示屬於該通訊群組清單中主管的每個成員的名稱和辦公室位置。 以下是這項程序的主要步驟:

  1. 下列程式碼範例會顯示 [ 選取通訊群組清單 ] 對話方塊,讓使用者選取通訊群組清單。

    它會使用 SelectNamesDialog 物件來顯示對話方塊並取得使用者選取專案。 The sample then obtains the user selection through the SelectNamesDialog.Recipients property.

  2. 對於選取之通訊群組清單中的每個成員:

    1. 如果成員是主管,程式碼範例便會顯示主管員的名稱和辦公室號碼。

    通訊群組清單中的每個成員都是 AddressEntry 物件。 藉由檢查 AddressEntry.AddressEntryUserType 是否為 olExchangeUserAddressEntryolExchangeRemoteUserAddressEntry,範例接著會將 AddressEntry 物件指派給 ExchangeUser 物件,並使用 ExchangeUser.GetDirectReports.Count >0 做為準則來判斷使用者是否為管理員。 然後,它會顯示ExchangeUser物件的NameOfficeLocation屬性。

  3. 如果成員是通訊群組清單,程式碼範例會呼叫副程式 EnumerateDLManagers 。 對於該通訊群組清單中的每個成員,如果成員是主管,程式碼範例便會顯示主管的名稱和辦公室號碼。

將下列 Visual Basic for Applications 程式碼範例複製到 Visual Basic 編輯器,然後執行 ShowManagersOfGroups 。 請注意,此程式碼範例僅適用于只有 Exchange 使用者作為成員的通訊群組清單,或是 Exchange 通訊群組清單為成員,但後者的所有成員都必須是 Exchange 使用者。 如果有更多巢狀的通訊群組清單作為成員,則需要進一步自訂程式碼。

Sub ShowManagersOfGroups() 
    Dim oRecip As Outlook.Recipient 
    Dim oSND As Outlook.SelectNamesDialog 
    Dim oAE As Outlook.AddressEntry 
    Dim oAEs As Outlook.AddressEntries 
    Dim oEU As Outlook.ExchangeUser 
    Dim oDL As Outlook.ExchangeDistributionList 
    Dim oLists As Outlook.AddressLists 
    Dim oList As Outlook.AddressList 
    Set oLists = Application.Session.AddressLists 
    For Each oList In oLists 
        If oList.Name = "All Groups" Then 
            Exit For 
        End If 
    Next 
    Set oSND = Application.Session.GetSelectNamesDialog 
    With oSND 
        .NumberOfRecipientSelectors = olShowTo 
        .InitialAddressList = oList 
        .Caption = "Select Distribution List" 
        .ToLabel = "D/L" 
        .ShowOnlyInitialAddressList = True 
        .AllowMultipleSelection = False 
        .Display 
    End With 
    For Each oRecip In oSND.Recipients 
        If oRecip.AddressEntry.AddressEntryUserType = _ 
            olExchangeDistributionListAddressEntry Then 
            Set oDL = oRecip.AddressEntry.GetExchangeDistributionList 
            Set oAEs = oDL.GetExchangeDistributionListMembers 
            For Each oAE In oAEs 
                If oAE.AddressEntryUserType = olExchangeUserAddressEntry _ 
                    Or oAE.AddressEntryUserType = olExchangeRemoteUserAddressEntry Then 
                    Set oEU = oAE.GetExchangeUser 
                    If oEU.GetDirectReports.Count Then 
                        Debug.Print oEU.Name, oEU.OfficeLocation 
                    End If 
                ElseIf oAE.AddressEntryUserType = _ 
                    olExchangeDistributionListAddressEntry Then 
                    EnumerateDLManagers oAE 
                End If 
            Next 
        End If 
    Next 
End Sub 
 
Sub EnumerateDLManagers(oAddress As AddressEntry) 
    Dim oAE As Outlook.AddressEntry 
    Dim oAEs As Outlook.AddressEntries 
    Dim oEU As Outlook.ExchangeUser 
    Dim oDL As Outlook.ExchangeDistributionList 
     
    Set oDL = oAddress.GetExchangeDistributionList 
    Set oAEs = oDL.GetExchangeDistributionListMembers 
    For Each oAE In oAEs 
        If oAE.AddressEntryUserType = olExchangeUserAddressEntry _ 
            Or oAE.AddressEntryUserType = olExchangeRemoteUserAddressEntry Then 
            Set oEU = oAE.GetExchangeUser 
            If oEU.GetDirectReports.Count Then 
                Debug.Print oEU.Name, oEU.OfficeLocation 
            End If 
        End If 
    Next 
End Sub

支援和意見反應

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