This topic has not yet been rated - Rate this topic

How to: Get the Account for a Folder

This example gets the account that is associated with a folder in the current session.

In the following code example, 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 Store property) with the default delivery store of each account (obtained with the DeliveryStore property) that is defined in the Accounts collection for the session. GetAccountForFolder returns the Account object if a match is found; otherwise, it returns a null reference.

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.


using Outlook = Microsoft.Office.Interop.Outlook;
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;

Other resources

Community Additions

© 2014 Microsoft. All rights reserved.