'How can Outlook addin detect when an account is removed

I get accounts from Outlook like below.

    Outlook.NameSpace ns = null;
    Outlook.Accounts accounts = null;
    Outlook.Account account = null;
    string accountList = string.Empty;

    try
    {
        ns = OutlookApp.Session;
        accounts = ns.Accounts;
        for (int i = 1; i <= accounts.Count; i++)
        {
            account = accounts[i];
            accountList += String.Format("{0} - {1}{2}", 
                account.UserName,  
                account.SmtpAddress, 
                Environment.NewLine);
            if (account != null)
                Marshal.ReleaseComObject(account);
        }
        MessageBox.Show(accountList);
    }
    finally
    {
        if (accounts != null)
            Marshal.ReleaseComObject(accounts);
        if (ns != null)
            Marshal.ReleaseComObject(ns);
    }

However, Outlook return accounts, including those that have been removed.
It seems that there are no events that occur when the account is removed.

After the account is removed, is there a way to get the accounts excluding the removed account? How I can get accounts excluding removed account?



Solution 1:[1]

On the MAPI level (C++ or Delphi), account events are implemented through IOlkAccountManager::Advise method. You can see the events fire in OutlookSpy (I am its author - click IOlkAccountManager button, go to the Advise tab.

Outlook Object Model does not expose these events. If using Redemption (I am also its author) is an option, it exposes all account events through the RDOAccounts object - AccountChange, AccountAdd, AccountRemove, AccountBeforeRemove, AccountOrderChange.

Solution 2:[2]

The Outlook object model doesn't provide any event for that. The best what you can do is to handle the Stores.BeforeStoreRemove event which is fired when a Store is about to be removed from the current session either programmatically or through user action. Here is what MSDN says for the event:


Outlook must be running in order for this event to fire. This event will fire when any of the following occurs:

  • A store is removed by the user clicking the Close command on the Shortcut menu.

  • A store is removed programmatically by calling Namespace.RemoveStore.

This event will not fire when any of the following occurs:

  • When Outlook shuts down and closes a primary or delegate store.

  • If a store is removed through the Mail applet in the Microsoft Windows Control Panel and Outlook is not running.

  • A delegate store is removed on the Advanced tab of the Microsoft Exchange Server dialog box.

  • A store is removed through the Data Files tab of the Account Manager dialog box when Outlook is not running.

  • An IMAP Store is removed from the profile.

You can use this event to determine that a store has been removed, and take appropriate actions if the store is required for your application (such as remounting the store). Otherwise you would have to resort to polling the Stores collection.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Eugene Astafiev