This documentation is archived and is not being maintained.

How to: Identify a Folder with an Account

In a Microsoft Outlook session that has multiple accounts defined in the profile, the folder that is displayed in the active explorer does not necessarily reside on the default store for that session; instead, it can reside on one of the multiple stores associated with the multiple accounts. This topic shows how to identify the account whose default delivery store is the same store that hosts the folder.

In the following code sample, the DisplayAccountForCurrentFolder function calls the GetAccountForFolder function to identify the account whose default delivery store hosts the current folder, and then displays the name of the folder. GetAccountForFolder matches the store of the current folder (obtained from the Folder.Store property) with the default delivery store of each account (obtained with the Account.DeliveryStore property) that is defined in the Accounts collection for the session. GetAccountForFolder returns the Account object when a match is found; otherwise, it returns null.

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 Microsoft Visual Studio and the Microsoft Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Microsoft Outlook 2010, ensure that you have installed the Outlook 2010 PIA and have added a reference to the Microsoft Outlook 14.0 Object Library component in Visual Studio. You should use the following code in the ThisAddIn class of a Visual Studio Tools for Office add-in for Outlook, such that the Application object in the code is 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 2010 Primary Interop Assembly Reference on MSDN.

private void DisplayAccountForCurrentFolder() 
    Outlook.Folder myFolder = Application.ActiveExplorer().CurrentFolder  
        as Outlook.Folder; 
    string msg = "Account for Current Folder:" + "\n" + 
Outlook.Account GetAccountForFolder(Outlook.Folder folder) 
    // Obtain the store on which the folder resides. 
    Outlook.Store store = folder.Store; 
    // Enumerate the accounts defined for the session. 
    foreach (Outlook.Account account in Application.Session.Accounts) 
        // Match the DefaultStore.StoreID of the account 
        // with the Store.StoreID for the currect folder. 
        if (account.DeliveryStore.StoreID  == store.StoreID) 
            // Return the account whose default delivery store 
            // matches the store of the given folder. 
            return account; 
     // No account matches, so return null. 
     return null;