列出 Exchange 通讯组列表中每个经理的姓名和办公室位置
本主题介绍如何允许用户选择 Exchange 通讯组列表,并显示属于该通讯组列表且是经理的每个成员的姓名和办公地点。 此过程的主要步骤如下所示:
下面的代码示例显示 “选择通讯组列表 ”对话框,供用户选择通讯组列表。
它使用 SelectNamesDialog 对象来显示对话框并获取用户选择。 然后,该示例通过 SelectNamesDialog.Recipients 属性获取用户选择。
对于所选通讯组列表中的每个成员:
- 如果该成员是经理,则该代码示例将显示该经理的姓名和办公室编号。
通讯组列表中的每个成员都是一个 AddressEntry 对象。 通过检查 AddressEntry.AddressEntryUserType 是 olExchangeUserAddressEntry 还是 olExchangeRemoteUserAddressEntry,示例随后将 AddressEntry 对象分配给 ExchangeUser 对象,并使用
ExchangeUser.GetDirectReports.Count >0
作为条件来确定用户是否为经理。 然后,它显示 ExchangeUser 对象的 Name 和 OfficeLocation 属性。如果该成员是通讯组列表,则代码示例将调用子例程
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 支持和反馈,获取有关如何接收支持和提供反馈的指南。