Identificar a lista de endereços global ou um conjunto de listas de endereços com uma loja

Em uma sessão do Microsoft Outlook em que várias contas do Microsoft Exchange são definidas no perfil, pode haver várias listas de endereços associadas a um repositório. Este tópico apresenta dois exemplos de código que mostram como recuperar a Lista de Endereços Global de um determinado repositório e como obter todos os objetos AddressList associados a um determinado repositório. Em cada um desses exemplos de código, o repositório específico de interesse é o repositório da pasta atual exibida no Explorer ativo, mas o algoritmo para obter uma Lista de Endereços Global ou um conjunto de listas de endereços para um repositório é aplicável a qualquer repositório do Exchange.

The following managed code is written in C#. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library. For Outlook, you can use Visual Studio and the Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2013, ensure that you have installed the Outlook 2013 PIA and have added a reference to the Microsoft Outlook 15.0 Object Library component in Visual Studio. Você deve usar o código a ThisAddIn seguir na classe de um suplemento do Outlook (usando ferramentas de desenvolvedor do Office para Visual Studio). The Application object in the code must be a trusted Outlook Application object provided by ThisAddIn.Globals. For more information about using the Outlook PIA to develop managed Outlook solutions, see the Outlook 2013 Primary Interop Assembly Reference on MSDN.

O primeiro exemplo de código contém o DisplayGlobalAddressListForStore método e a GetGlobalAddressList função. O DisplayGlobalAddressListForStore método exibe a Lista de Endereços Global associada ao repositório atual na caixa de diálogo Selecionar Nomes . DisplayGlobalAddressListForStore primeiro obtém o repositório atual. Se o repositório atual for um exchange store, chamará GetGlobalAddressList para obter a Lista de Endereços Global associada ao repositório atual. GetGlobalAddressList usa o objeto PropertyAccessor e a propriedade MAPI, https://schemas.microsoft.com/mapi/proptag/0x3D150102, para obter as interfaces dos usuários de uma lista de endereços e do repositório atual. GetGlobalAddressList identifica uma lista de endereços associada a um repositório se suas IUDs corresponderem e a lista de endereços for a Lista de Endereços Global se sua propriedade AddressListType for olExchangeGlobalAddressList. Se a chamada para GetGlobalAddressList ser bem-sucedida, DisplayGlobalAddressListForStore usará o objeto SelectNamesDialog para exibir a Lista de Endereços Global retornada na caixa de diálogo Selecionar Nomes .

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; 
} 

O segundo exemplo de código contém o método e GetAddressLists a EnumerateAddressListsForStore função. O EnumerateAddressListsForStore método exibe a ordem de tipo e resolução de cada lista de endereços definida para o repositório atual. EnumerateAddressListsForStoreprimeiro obtém o repositório atual, em seguida, ele chama GetAddressLists para obter um objeto list genérico .NET Framework que contém objetos AddressList para o repositório atual. GetAddressLists enumera cada lista de endereços definida para a sessão, usa o objeto PropertyAccessor e a propriedade mapi nomeada, https://schemas.microsoft.com/mapi/proptag/0x3D150102, para obter as interfaces dos usuários de uma lista de endereços e do repositório atual. GetGlobalAddressList identifica uma lista de endereços associada a um repositório se suas IUDs corresponderem e retornar um conjunto de listas de endereços para o repositório atual. EnumerateAddressListsForStore em seguida, usa as propriedades AddressListType e ResolutionOrder do objeto AddressList para exibir o tipo e a ordem de resolução de cada lista de endereços retornada.

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; 
} 

Suporte e comentários

Tem dúvidas ou quer enviar comentários sobre o VBA para Office ou sobre esta documentação? Confira Suporte e comentários sobre o VBA para Office a fim de obter orientação sobre as maneiras pelas quais você pode receber suporte e fornecer comentários.