简体   繁体   中英

How to Execute Event on VSTO Add In Once Outlook is loaded C#

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
}

Application 类的Startup事件在 Microsoft Outlook 启动时触发,但在所有加载项都已加载之后。

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

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

this.Application.ActiveDocument.DocumentChanged += MyEventHandler;

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

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

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

reference: 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 -- (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 <-- i deleted this registry entry

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM