Identifizieren der globalen Adressliste oder einer Gruppe von Adresslisten mit einem Speicher

In a Microsoft Outlook session where multiple Microsoft Exchange accounts are defined in the profile, there can be multiple address lists associated with a store. This topic has two code samples that show how to retrieve the Global Address List for a given store, and how to obtain all of the AddressList objects associated with a given store. In jedem dieser Codebeispiele ist der spezifische Speicher von Interesse der Speicher für den aktuellen Ordner, der im aktiven Explorer angezeigt wird, aber der Algorithmus zum Abrufen einer globalen Adressliste oder einer Reihe von Adresslisten für einen Speicher gilt für jeden Exchange-Speicher.

Der folgende verwaltete Code ist in C# geschrieben. Um ein verwaltetes Codebeispiel von .NET Framework auszuführen, das ein Component Object Model (COM) aufrufen muss, müssen Sie eine Interopassembly verwenden, die verwaltete Schnittstellen definiert und den COM-Objekten in der Object Model-Typbibliothek zuordnet. Für Outlook können Sie Visual Studio und die Outlook Primary Interop Assembly (PIA) verwenden. Stellen Sie sicher, dass Sie die Outlook 2013 PIA installiert und eine Referenz zur Microsoft Outlook 15.0-Objektbibliothekkomponente in Visual Studio hinzugefügt haben, bevor Sie verwaltete Codebeispiele für Outlook 2013 ausführen. Sie sollten den folgenden Code in der ThisAddIn Klasse eines Outlook-Add-Ins verwenden (mit Office Developer Tools für Visual Studio). Das Objekt der Anwendung im Code muss ein vertrauenswürdiges Outlook- Anwendungsobjekt sein, das von ThisAddIn.Globals bereitgestellt wird. Weitere Informationen zur Verwendung der Outlook-PIA zur Entwicklung verwalteter Outlook-Lösungen finden Sie auf MSDN unter Willkommen bei der Referenz zur primären Interopassembly von Outlook (PIA).

Das erste Codebeispiel enthält die DisplayGlobalAddressListForStore -Methode und die GetGlobalAddressList -Funktion. The DisplayGlobalAddressListForStore method displays the Global Address List that is associated with the current store in the Select Names dialog box. DisplayGlobalAddressListForStore first obtains the current store. If the current store is an Exchange store, calls GetGlobalAddressList to obtain the Global Address List associated with the current store. GetGlobalAddressList verwendet das PropertyAccessor-Objekt und die MAPI-Eigenschaft , https://schemas.microsoft.com/mapi/proptag/0x3D150102um die UIDs einer Adressliste und des aktuellen Speichers abzurufen. GetGlobalAddressList identifiziert eine Adressliste, die einem Speicher zugeordnet ist, wenn ihre UIDs übereinstimmen, und die Adressliste ist die globale Adressliste, wenn ihre AddressListType-EigenschaftolExchangeGlobalAddressList ist. Wenn der Aufruf von GetGlobalAddressList erfolgreich ist, wird das SelectNamesDialog-Objekt verwendet, DisplayGlobalAddressListForStore um die zurückgegebene globale Adressliste im Dialogfeld Namen auswählen anzuzeigen.

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

Das zweite Codebeispiel enthält die -Methode und GetAddressLists -EnumerateAddressListsForStoreFunktion. The EnumerateAddressListsForStore method displays the type and resolution order of each address list defined for the current store. EnumerateAddressListsForStore first obtains the current store, then it calls GetAddressLists to obtain a .NET Framework generic List object that contains AddressList objects for the current store. GetAddressLists Listet jede für die Sitzung definierte Adressliste auf, verwendet das PropertyAccessor-Objekt und die benannte MAPI-Eigenschaft , https://schemas.microsoft.com/mapi/proptag/0x3D150102um die UIDs einer Adressliste und des aktuellen Speichers abzurufen. GetGlobalAddressList identifies an address list as associated with a store if their UIDs match, and returns a set of address lists for the current store. Anschließend verwendet EnumerateAddressListsForStore die Eigenschaften AddressListType und ResolutionOrder des AddressList-Objekts, um den Typ und die Auflösungsreihenfolge für jede zurückgegebene Adressliste anzuzeigen.

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

Support und Feedback

Haben Sie Fragen oder Feedback zu Office VBA oder zu dieser Dokumentation? Unter Office VBA-Support und Feedback finden Sie Hilfestellung zu den Möglichkeiten, wie Sie Support erhalten und Feedback abgeben können.