首页 文章

Outlook VSTO 无法在 NewMailEx 事件上触发?

提问于
浏览
2

我已经在 C#中创建了一个 VSTO,它应该钩住 Outlook 2007 的 NewMailEx 事件。但是,当我进行手动 send/receive 或收件箱中只有 1 封未读邮件时,有时不会触发。在邮件实际到达之前,它似乎好像在收件箱中触发了。

除了使用 VSTO 的 ItemAdd 或 NewMailEX 之外,是否还有更好的方法可以“每次”监视新消息?

1 回答

  • 3

    原因是:“ GC 收集了.NET 对象,该对象从 Outlook 中包装了 COM 对象”)。解决方案是对此.NET 对象的保留引用。最简便的方法是:

    // this is helper collection.
    // there are all wrapper objects
    // , which should not be collected by GC
    private List<object> holdedObjects = new List<object>();
    
    // hooks necesary events
    void HookEvents() {
        // finds button in commandbars
        CommandBarButton btnSomeButton = FindCommandBarButton( "MyButton ");
        // hooks "Click" event
        btnSomeButton.Click += btnSomeButton_Click;
        // add "btnSomeButton" object to collection and
        // and prevent themfrom collecting by GC
        holdedObjects.Add( btnSomeButton );
    }
    

    如果需要,您还可以为此(和其他)具体按钮(或其他对象)有一个特殊字段。但这是最常见的解决方案。

相关问题