ストアを使用してグローバル アドレス一覧またはアドレス一覧のセットを特定する

プロファイルで複数の Microsoft Exchange アカウントが定義されている Microsoft Outlook セッションでは、ストアに複数のアドレス一覧を関連付けることができます。 ここでは、指定したストアのグローバル アドレス一覧を取得する方法と、指定したストアに関連付けられているすべての AddressList オブジェクトを取得する方法を説明する 2 つのコード例を示します。 これらのコード例では、アクティブなエクスプローラーに表示されている現在のフォルダーのストアを対象として指定していますが、ストアのグローバル アドレス一覧またはアドレス一覧のセットを取得するアルゴリズムは、すべての Exchange ストアに適用できます。

次のマネージ コードは C# で作成されています。 コンポーネント オブジェクト モデル (COM) に呼び出す必要がある .NET Framework マネージ コード サンプルを実行するには、マネージ インターフェイスを定義し、オブジェクト モデル タイプ ライブラリの COM オブジェクトにマップする相互運用機能アセンブリを使用する必要があります。 Outlook の場合、Visual Studio および Outlook プライマリ相互運用機能アセンブリ (PIA) を使用できます。 Outlook 2013 用のマネージ コード サンプルを実行する前に、Outlook 2013 PIA をインストールしており、Visual Studio で Microsoft Outlook 15.0 オブジェクト ライブラリ コンポーネントへの参照を追加していることを確認してください。 Outlook アドインのクラスで次のコードを ThisAddIn 使用する必要があります (Office Developer Tools for Visual Studio を使用)。 コードの Application オブジェクトは で提供された、信頼済み Outlook ThisAddIn.Globals オブジェクトである必要があります。 Outlook PIA を使用してマネージド Outlook ソリューションを開発する方法の詳細については、MSDN の 「Outlook プライマリ相互運用機能アセンブリ リファレンスへようこそ」を参照 してください。

最初のコード サンプルには、 DisplayGlobalAddressListForStore メソッドと 関数が GetGlobalAddressList 含まれています。 メソッドは DisplayGlobalAddressListForStore 、[ 名前の選択 ] ダイアログ ボックスで現在のストアに関連付けられているグローバル アドレス一覧を表示します。 DisplayGlobalAddressListForStore は最初に現在のストアを取得します。 現在のストアが Exchange ストアの場合は、 を呼び出 GetGlobalAddressList して、現在のストアに関連付けられているグローバル アドレス一覧を取得します。 GetGlobalAddressListPropertyAccessor オブジェクトと MAPI プロパティ を使用して、https://schemas.microsoft.com/mapi/proptag/0x3D150102アドレス一覧と現在のストアの UID を取得します。 GetGlobalAddressList は、UID が一致する場合はストアに関連付けられているアドレス一覧を識別し、 AddressListType プロパティが olExchangeGlobalAddressList の場合はアドレス一覧がグローバル アドレス一覧になります。 呼 GetGlobalAddressList び出しが成功した場合は、 DisplayGlobalAddressListForStoreSelectNamesDialog オブジェクトを使用して、返されたグローバル アドレス一覧を [ 名前の選択 ] ダイアログ ボックスに表示します。

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

2 番目のコード サンプルには、 メソッドとGetAddressLists関数がEnumerateAddressListsForStore含まれています。 メソッドは EnumerateAddressListsForStore 、現在のストアに対して定義されている各アドレス一覧の型と解決順序を表示します。 EnumerateAddressListsForStore最初に現在のストアを取得し、 を呼び出GetAddressListsして、現在のストアの AddressList オブジェクトを含む.NET Frameworkジェネリック List オブジェクトを取得します。 GetAddressLists は、セッションに対して定義されている各アドレス一覧を列挙し、 PropertyAccessor オブジェクトと MAPI という名前のプロパティ を使用して、 https://schemas.microsoft.com/mapi/proptag/0x3D150102アドレス一覧と現在のストアの UID を取得します。 GetGlobalAddressList は、UID が一致する場合にストアに関連付けられているアドレス一覧を識別し、現在のストアのアドレス一覧のセットを返します。 その後、 EnumerateAddressListsForStore は、 AddressList オブジェクトの AddressListType プロパティと ResolutionOrder プロパティを使用して、返された各アドレス一覧の種類と解決順序を表示します。

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 のサポートおよびフィードバックを参照してください。