如何退出VSTO插件

5

有没有比在add-in代码中抛出异常更好的方法来关闭Outlook 2010的VSTO add-in?
我不想抛出异常,因为Outlook可能会认为我的add-in不稳定。


---编辑:---
关闭的意思是停止add-in代码的执行并隐藏其UI或停用它。但我希望在Outlook重新启动后它仍然可用。


你说的“close”是指什么?是禁用它?还是关闭一个面板? - Karl-Johan Sjögren
我所说的“关闭”,是指停止执行插件代码并隐藏其用户界面或将其停用。但我希望在 Outlook 重新启动后它可以被启用。 - Andriy Kozachuk
2个回答

3
当你在VS2010中创建你的VSTO项目时,以下代码应该会自动在你的ThisAddIn.cs文件中生成。如果没有生成,你可能需要自己添加它们。
/// <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);
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
     //execute your code here, e.g. output some values to a text file 
}

您可以将代码放置在ThisAddIn_Shutdown事件中,并且只在插件关闭时执行它。
编辑: 以下是MSDN的说法:
从Outlook 2010开始,默认情况下,Outlook不会向插件发出正在关闭的信号。具体来说,Outlook在快速关闭期间不再调用IDTExtensibility2接口的OnBeginShutdown和OnDisconnection方法。同样,使用Microsoft Visual Studio Tools for Office编写的Outlook插件在Outlook关闭时也不再调用ThisAddin_Shutdown方法。
更多细节请参阅:
http://msdn.microsoft.com/en-us/library/office/ee720183.aspx#OL2010AdditionalShutdownChanges_AddinShutdownChangesinOL2010Beta

谢谢您的回答,但我实际想要的是触发ThisAddIn_Shutdown事件。您有什么想法吗? - Andriy Kozachuk

3

试试这个:

public void UnloadAddInManually()
{
    //Try find our Add-In...
    foreach (COMAddIn comAddIn in this.Application.COMAddIns)
    {
        if (comAddIn.ProgId.Contains("< ADDIN_PROG_ID >"))
        {
            //Found Add-In: Unload it...
            comAddIn.Connect = false;
            break;
        }
    }
}

这对我在EXCEL中有效。

注意:您可以在“设置>加载项>COM加载项”下查看您的< ADDIN_PROG_ID >,以便在办公室主机应用程序中找到它。


太好了!我使用这段代码来关闭我的插件,以防有人拒绝最终用户许可协议;-) - Jan Rothkegel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接