C# - 希望在 Outlook 中添加/删除商店时捕获事件

发布于 2024-09-27 15:54:49 字数 2212 浏览 0 评论 0原文

我正在编写一个加载项,当通过“数据文件管理”菜单或通过 AddStore/RemoveStore 添加/删除 PST 时需要进行记录。我一直很难找到有关如何捕获这些事件的文档。

非常感谢您的建议。

谢谢, 拉里

编辑:这是我的无效代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你不是我要的菜∠ 2024-10-04 15:54:49

像这样:

...

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

...

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

...

So要识趣 2024-10-04 15:54:49

Outlook Session 对象有 2 个用于添加和删除 pststore 的事件 -

  1. StoreAdd
  2. BeforeStoreRemove

您可以订阅这些事件,以便在添加或删除任何 pst 时获取通知。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文