0

J'écris un complément qui doit se connecter lorsqu'un PST est ajouté/supprimé via le menu "Data File Management" ou via AddStore/RemoveStore . J'ai eu de la difficulté à trouver de la documentation sur la façon de capturer ces événements.C# - Vous cherchez à capturer un événement lorsqu'un magasin est ajouté/supprimé dans Outlook

Un conseil est grandement apprécié.

Merci, Larry

EDIT: Voici mon code fictif qui ne fonctionne pas:

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

Répondre

0

Perspectives session objet a 2 événements pour ajouter et supprimer pststore -

  1. StoreAdd
  2. BeforeStoreSupprimer

Vous pouvez vous abonner à cet événement pour obtenir la notofication chaque fois qu'un élément pst est ajouté ou supprimé.

+0

J'ai écrit un programme « code fictif » pour tester, mais je suis seulement obtenir le succès lors du passage à travers les fonctions AddStore/RemoveStore et de la " Ouvrir -> Fichier de données Outlook "option de menu. Rien n'est déclenché lors de l'utilisation du DFM. –

+0

Kaplig: J'ai déjà souscrit à ces événements mais rien ne se passe lorsque j'utilise le menu DFM. Je posterai mon "code fictif" pour que vous puissiez voir de quoi je parle. –

0

Comme ceci:

... 

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

...

+0

Merci, MLJ. J'ai résolu le problème il y a quelques mois. C'était juste des fautes de frappe de ma part. –