如何在VSTO上执行事件添加一旦Outlook加载C#

时间:2017-10-25 10:42:07

标签: c# vsto outlook-addin

如何在Outlook应用程序完全加载后执行事件。我试图在C#VSTO addin启动事件触发时执行一些代码,但我希望在应用程序加载完成后运行脚本。有什么想法吗?

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {

        //Doesn't make sense to add script here because outlook is still not finished loading
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        // Note: Outlook no longer raises this event. If you have code that 
        //    must run when Outlook shuts down, see http://go.microsoft.com/fwlink/?LinkId=506785
    }

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

2 个答案:

答案 0 :(得分:0)

在Microsoft Outlook启动时会激活Application类的Startup事件,但是在加载所有加载项之后。

答案 1 :(得分:0)

为了后代,我每天都使用堆栈,但从未发布过。 我为 Visio 维护了一个 VSTO 加载项,它与其他 Office 加载项具有共同的相关性...

我也在尝试在加载项启动事件中运行代码:ThisAddIn_Startup。

this.Application.ActiveDocument.DocumentChanged += MyEventHandler;

我一直收到一个异常:值不能为空。参数名称:o

我发现,虽然加载了加载项,但没有打开的文档: this.Application.ActiveDocument 为空

我将事件处理程序分配更改为: this.Application.DocumentOpened += MyHigherLayerEventHandler;

参考: https://docs.microsoft.com/en-us/visualstudio/vsto/programming-vsto-add-ins?view=vs-2019#AccessingDocuments https://docs.microsoft.com/en-us/visualstudio/vsto/events-in-office-projects?view=vs-2019 https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.addinbase.requestcomaddinautomationservice?view=vsto-2017 https://docs.microsoft.com/en-us/visualstudio/vsto/architecture-of-vsto-add-ins?view=vs-2019

THEN --(这部分可能需要在一个单独的主题中,idk,但是) 我注意到该事件触发了两次。事实上, ThisAddIn_Startup --and-- ThisAddIn_Shutdown 都触发了 2 次。这个周末我花了大约 10 个小时试图解决这个问题。我什至添加了一个带有消息框的增量计数器:消息框弹出 2 次,但计数器没有增加。

问题/解决方案原来是注册表项。 如果您像我这样的开发人员,您的机器可能有多个测试项目,也许还有一些废弃的旧项目。我有一个具有相同清单文件名但存储库位置不同的旧加载项。在通过注册表进行几次查找搜索后

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Visio\Addins\TEAM_VSTO <-- 我删除了这个注册表项

计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Visio\Addins\NewAddinName

*presto, magic, event 只触发 1 次 *facepalm 无论如何,我希望这对某人有所帮助。