0

I am writing an add-in that needs to log when a PST is added/removed via the "Data File Management" menu or through AddStore/RemoveStore. I have been having difficulty on finding documentation on how to capture these events.

Advice is greatly appreciated.

Thanks, Larry

EDIT: Here's my dummy code that's not working:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;

namespace StoreTesting
{
    public partial class ThisAddIn
    {
        Outlook.Application olApp = new Outlook.ApplicationClass();
        Outlook.NameSpace ns;

        Outlook.Stores stores;
        int open;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            ns = olApp.GetNamespace("MAPI");


            stores = ns.Stores;
            open = 0;

            foreach (Outlook.MAPIFolder mf in stores.Session.Folders)
                if (mf.Store.IsDataFileStore)
                    open += 1;

            stores.StoreAdd += new Outlook.StoresEvents_12_StoreAddEventHandler(stores_StoreAdd);
            stores.BeforeStoreRemove += new Outlook.StoresEvents_12_BeforeStoreRemoveEventHandler(stores_BeforeStoreRemove);

        }

        void stores_BeforeStoreRemove(Outlook.Store Store, ref bool Cancel)
        {

            string rf = string.Format("{0}:{1} was removed", Store.DisplayName);
            MessageBox.Show(rf);
            open -= 1;
        }

        void stores_StoreAdd(Outlook.Store Store)
        {
            Outlook.MAPIFolder mf = ns.Folders.GetLast();
            string af = string.Format("{0} was added", mf.Name);
            MessageBox.Show(af);
            open += 1;
        }

        void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
Larry G. Wapnitsky
  • 1,216
  • 2
  • 16
  • 35

2 Answers2

0

Outlook Session object has 2 events for adding and removing pststore -

  1. StoreAdd
  2. BeforeStoreRemove

You can subscribe to these event to get the notofication whenever any pst is added or removed.

Kapil
  • 9,469
  • 10
  • 40
  • 53
  • I've written a "dummy code" program to test this, but am only getting success when going through the AddStore/RemoveStore functions and the "Open -> Outlook Data File" menu option. Nothing is triggered when using the DFM. – Larry G. Wapnitsky Oct 15 '10 at 13:56
  • Kaplig: I've already subscribed to those events but nothing is happening when I use the DFM menu. I'll post my "dummy code" so you can see what I'm referring to. – Larry G. Wapnitsky Oct 18 '10 at 12:13
0

Like this:

...

    private void ThisAddInStartup(object sender, EventArgs e)
    {
        this.Application.Session.Stores.StoreAdd += StoreAddEventHandler;
        this.Application.Session.Stores.BeforeStoreRemove += BeforeStoreRemove;
    }

    private void StoreAddEventHandler(Store store)
    {
        if (store.IsDataFileStore)
        {
          //Do something.
        }
    }

    private void BeforeStoreRemove(Store store, ref bool cancel)
    {
        if (store.IsDataFileStore)
        {
            //Do something.
        }
    }

...

MLJ
  • 123
  • 2
  • 11