使用应用商店标识全局地址列表或一组地址列表

在配置文件中定义了多个 Microsoft Exchange 帐户的 Microsoft Outlook 会话中,可以有多个与存储相关的地址列表。 本主题有两个代码示例,演示如何检索给定存储的全球通讯簿,以及如何获取与给定存储关联的所有 AddressList 对象。 在这两个代码示例中,所使用的特定存储均为活动资源管理器中显示的当前文件夹的存储,但这个用于获取某个存储的全球通讯簿或一组地址列表的算法适用于任何 Exchange 存储。

下面的托管代码是使用 C# 编写的。 若要运行需要调入组件对象模型 (COM) 的 .NET Framework 托管代码示例,您必须使用可定义托管接口并将其映射到对象模型类型库中的 COM 对象的互操作程序集。 对于 Outlook,您可以使用 Visual Studio 和 Outlook 主互操作程序集 (PIA)。 在您运行适用于 Outlook 2013 的托管代码示例之前,请确保您已安装了 Outlook 2013 PIA 并且已添加了对 Visual Studio 中的 Microsoft Outlook 15.0 对象库组件的引用。 应使用 Office Developer Tools for Visual Studio) 在 Outlook 外接程序 (类中使用以下代码 ThisAddIn 。 代码中的 应用程序对象必须是由 提供的受信任 Outlook ThisAddIn.Globals对象。 有关使用 Outlook PIA 开发托管 Outlook 解决方案的详细信息,请参阅欢迎使用 MSDN 上的 Outlook 主互操作程序集参考

第一个代码示例包含 DisplayGlobalAddressListForStore 方法和 GetGlobalAddressList 函数。 方法 DisplayGlobalAddressListForStore 在“ 选择名称 ”对话框中显示与当前存储关联的全局地址列表。 DisplayGlobalAddressListForStore first obtains the current store. 如果当前存储是 Exchange 存储,则调用 GetGlobalAddressList 以获取与当前存储关联的全局地址列表。 GetGlobalAddressList 使用 PropertyAccessor 对象和 MAPI 属性 https://schemas.microsoft.com/mapi/proptag/0x3D150102获取地址列表和当前存储的 UID。 GetGlobalAddressList 如果存储的 UID 匹配,则标识与存储关联的地址列表,如果其 AddressListType 属性 为 olExchangeGlobalAddressList,则地址列表为全局地址列表。 如果调用 GetGlobalAddressList 成功, DisplayGlobalAddressListForStore 请使用 SelectNamesDialog 对象在“ 选择名称 ”对话框中显示返回的全局地址列表。

void DisplayGlobalAddressListForStore() 
{ 
    // Obtain the store for the current folder 
    // as the current store. 
    Outlook.Folder currentFolder = 
        Application.ActiveExplorer().CurrentFolder 
        as Outlook.Folder; 
    Outlook.Store currentStore = currentFolder.Store; 
 
    // Check if the current store is Exchange. 
    if (currentStore.ExchangeStoreType != 
        Outlook.OlExchangeStoreType.olNotExchange) 
    { 
        Outlook.SelectNamesDialog snd =  
            Application.Session.GetSelectNamesDialog(); 
 
        // Try to get the Global Address List associated  
        // with the current store. 
        Outlook.AddressList addrList =  
            GetGlobalAddressList(currentStore); 
        if (addrList != null) 
        { 
            // Display the Global Address List in the  
            // Select Names dialog box. 
            snd.InitialAddressList = addrList; 
            snd.Display(); 
        } 
    } 
} 
 
public Outlook.AddressList GetGlobalAddressList(Outlook.Store store) 
{ 
    // Property string for the UID of a store or address list. 
    string  PR_EMSMDB_SECTION_UID =  
        @"https://schemas.microsoft.com/mapi/proptag/0x3D150102"; 
 
    if (store == null) 
    { 
        throw new ArgumentNullException(); 
    } 
 
    // Obtain the store UID using the property string and  
    // property accessor on the store. 
    Outlook.PropertyAccessor oPAStore = store.PropertyAccessor; 
 
    // Convert the store UID to a string value. 
    string storeUID = oPAStore.BinaryToString( 
        oPAStore.GetProperty(PR_EMSMDB_SECTION_UID)); 
 
    // Enumerate each address list associated 
    // with the session. 
    foreach (Outlook.AddressList addrList  
        in Application.Session.AddressLists) 
    { 
        // Obtain the address list UID and convert it to  
        // a string value. 
        Outlook.PropertyAccessor oPAAddrList =  
            addrList.PropertyAccessor; 
        string addrListUID = oPAAddrList.BinaryToString( 
            oPAAddrList.GetProperty(PR_EMSMDB_SECTION_UID)); 
 
        // Return the address list associated with the store 
        // if the address list UID matches the store UID and 
        // type is olExchangeGlobalAddressList. 
        if (addrListUID == storeUID && addrList.AddressListType == 
            Outlook.OlAddressListType.olExchangeGlobalAddressList) 
        { 
            return addrList; 
        } 
    } 
    return null; 
} 

第二个代码示例包含 EnumerateAddressListsForStore 方法和 GetAddressLists 函数。 EnumerateAddressListsForStore 方法显示为当前存储定义的每个地址列表的类型和解析顺序。 首先, EnumerateAddressListsForStore 获取当前存储,然后,它调用 GetAddressLists 来获取包含当前存储的 AddressList 对象的 .NET Framework 泛型 List 对象。 GetAddressLists 枚举为会话定义的每个地址列表,使用 PropertyAccessor 对象和 MAPI 命名属性 https://schemas.microsoft.com/mapi/proptag/0x3D150102获取地址列表和当前存储的 UID。 如果它们的 UID 相匹配,则 GetGlobalAddressList 将该地址列表标识为与当前存储相关联,并返回当前存储的一组地址列表。 EnumerateAddressListsForStore然后使用 AddressList 对象的 AddressListTypeResolutionOrder 属性显示每个返回地址列表的类型和解析顺序。

private void EnumerateAddressListsForStore() 
{ 
    // Obtain the store for the current folder 
    // as the current store. 
    Outlook.Folder currentFolder = 
       Application.ActiveExplorer().CurrentFolder 
       as Outlook.Folder; 
    Outlook.Store currentStore = currentFolder.Store; 
 
    // Obtain all address lists for the current store. 
    List<Outlook.AddressList> addrListsForStore =  
        GetAddressLists(currentStore); 
    foreach (Outlook.AddressList addrList in addrListsForStore) 
    { 
        // Display the type and resolution order of each  
        // address list in the current store. 
        Debug.WriteLine(addrList.Name  
            + " " + addrList.AddressListType.ToString() 
            + " Resolution Order: " + 
            addrList.ResolutionOrder); 
     }  
} 
 
public List<Outlook.AddressList> GetAddressLists(Outlook.Store store) 
{ 
    List<Outlook.AddressList> addrLists =  
        new List<Microsoft.Office.Interop.Outlook.AddressList>(); 
 
    // Property string for the UID of a store or address list. 
    string PR_EMSMDB_SECTION_UID = 
        @"https://schemas.microsoft.com/mapi/proptag/0x3D150102"; 
 
    if (store == null) 
    { 
        throw new ArgumentNullException(); 
    } 
 
    // Obtain the store UID and convert it to a string value. 
    Outlook.PropertyAccessor oPAStore = store.PropertyAccessor; 
    string storeUID = oPAStore.BinaryToString( 
        oPAStore.GetProperty(PR_EMSMDB_SECTION_UID)); 
 
    // Enumerate each address list associated 
    // with the session. 
    foreach (Outlook.AddressList addrList 
        in Application.Session.AddressLists) 
    { 
        // Obtain the address list UID and convert it to  
        // a string value. 
        Outlook.PropertyAccessor oPAAddrList = 
            addrList.PropertyAccessor; 
        string addrListUID = oPAAddrList.BinaryToString( 
            oPAAddrList.GetProperty(PR_EMSMDB_SECTION_UID)); 
         
        // Add the address list to the resultant set of address lists 
        // if the address list UID matches the store UID. 
        if (addrListUID == storeUID) 
        { 
            addrLists.Add(addrList); 
        } 
    } 
    // Return the set of address lists associated with the store. 
    return addrLists; 
} 

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。