加载 Outlook 后如何在 VSTO 插件上执行事件 C# [英] How to Execute Event on VSTO Add In Once Outlook is loaded C#

查看:94
本文介绍了加载 Outlook 后如何在 VSTO 插件上执行事件 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I execute an event after the Outlook application is fully loaded. I tried to execute some code when the C# VSTO addin startup event fires but I am looking to run a script after the application is finished loading. Any ideas?

        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
}

推荐答案

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

For posterity and I use the stack everyday but have never posted. I maintain a VSTO Add-in for Visio which has common relevance to other Office Add-ins...

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

I too am trying to run code in the Add-in Startup Event: ThisAddIn_Startup.

this.Application.ActiveDocument.DocumentChanged += MyEventHandler;

this.Application.ActiveDocument.DocumentChanged += MyEventHandler;

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

I kept getting an exception: Value cannot be null. Parameter name: o

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

What I found is that, although the add-in is loaded there are no open documents: this.Application.ActiveDocument is null

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

I changed my event handler assignment to: this.Application.DocumentOpened += MyHigherLayerEventHandler;

参考:https://docs.microsoft.com/en-us/visualstudio/vsto/programming-vsto-add-ins?view=vs-2019#AccessingDocumentshttps://docs.microsoft.com/en-us/visualstudio/vsto/events-in-office-projects?view=vs-2019https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.addinbase.requestcomaddinautomationservice?view=vsto-2017https://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 次,但计数器没有增加.

THEN -- (this part might need to be in a separate topic, idk, but) I noticed that the event was firing twice. As a matter of fact both ThisAddIn_Startup --and-- ThisAddIn_Shutdown fire 2 times. I spent about 10 hours this weekend trying to solve this issue. I even added an increment counter with a messagebox : the messagebox popped up 2 times and the counter did NOT increment.

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

The problem / solution turned out to be registry entries. If you are anything like me as a developer your machine probably has more than a couple of test projects and maybe some old abandoned ones. I had an older Add-in of the same manifest file name but a different repository location. After a couple of find searches through the registry

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

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Visio\Addins\TEAM_VSTO <-- i deleted this registry entry

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

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Visio\Addins\NewAddinName

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

*presto, magic, event only fires 1 time *facepalm Anyway, I hope this helps someone.

这篇关于加载 Outlook 后如何在 VSTO 插件上执行事件 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆