1

经过几个小时的实验和谷歌搜索后,我终于完成了我自己能弄清楚的事情,所以这就是我现在所拥有的:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Application.Startup += new
        Outlook.ApplicationEvents_11_StartupEventHandler(
        ApplicationObject_Startup);
    ((Outlook.ApplicationEvents_11_Event)Application).Quit += new
        Outlook.ApplicationEvents_11_QuitEventHandler(
        ApplicationObject_Quit);
}
void ApplicationObject_Startup()
{
    MessageBox.Show("Startup Event");
    ((Outlook.ExplorerEvents_10_Event)Application.ActiveExplorer()).Close += new
        Outlook.ExplorerEvents_10_CloseEventHandler(
        ExplorerObject_Close);
}

void ApplicationObject_Quit()
{
    MessageBox.Show("Quit Event");
}

void ExplorerObject_Close()
{
    MessageBox.Show("Explorer Close Event");
}

所有这些都有效,当我关闭 Outlook 时,我会按顺序看到资源管理器关闭事件和退出事件消息框。但是,此时,前景似乎已经关闭,我不知道如何取消这些事件(对于其他一些事件,有一个 bool Cancel 传入,您可以将其设置为 false,但对于这些事件不可以?),或发送最小化事件(我根本无法弄清楚这一点)。

如果有人有任何建议,我将不胜感激。我在工作中有一些空闲时间,我想我会尝试学习一些插件开发的东西,同时解决 Outlook 中一个非常烦人的部分!

编辑:我也试过:

Application.ActiveExplorer().WindowState = Outlook.OlWindowState.olMinimized;

在启动时立即最小化窗口。它确实最小化了前景,但不是系统托盘,只是到栏(这很有趣,实际上可能是一个错误,因为最小化设置为最小化到托盘......)不过,如果我能摆脱关闭/quit event(s) 我至少可以将窗口最小化到任务栏。

4

1 回答 1

2
private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        try
        {
            // Assign startup and quit events
            Application.Startup += new Outlook.ApplicationEvents_11_StartupEventHandler(ApplicationObject_Startup);
            ((Outlook.ApplicationEvents_11_Event)Application).Quit += new Outlook.ApplicationEvents_11_QuitEventHandler(ApplicationObject_Quit);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "EXCEPTION", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        try
        {
            // Remove the startup and quit events
            Application.Startup -= new Outlook.ApplicationEvents_11_StartupEventHandler(ApplicationObject_Startup);
            ((Outlook.ApplicationEvents_11_Event)Application).Quit -= new Outlook.ApplicationEvents_11_QuitEventHandler(ApplicationObject_Quit);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "EXCEPTION", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    void ApplicationObject_Startup()
    {
        try
        {
            // Minimize to taskbar
            Application.ActiveExplorer().WindowState = Outlook.OlWindowState.olMinimized;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "EXCEPTION", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    void ApplicationObject_Quit()
    {
        try
        {
            // Restart outlook minimized
            ProcessStartInfo psiOutlook = new ProcessStartInfo("OUTLOOK.EXE", "/recycle");
            psiOutlook.WindowStyle = ProcessWindowStyle.Minimized;
            Process.Start(psiOutlook);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "EXCEPTION", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

它不是很干净,但这对我有用。它只是在您关闭 Outlook 时启动一个新实例,并始终在启动时最小化 Outlook。当然,主要缺点是,如果不先禁用加载项或在任务管理器中杀死“outlook.exe”,您将无法真正关闭 Outlook。不是最好的解决方案,但它确实可以确保您永远不会因为意外关闭 Outlook 而错过电子邮件或日历提醒。

编辑(2012 年 10 月 2 日):我更新了代码的重启前景部分。它现在使用 /recycle 开关启动 Outlook.exe。这会尝试在现有窗口中重新加载 Outlook。它还使用最小化窗口样式重新启动 Outlook。这可以防止出现 Outlook 的加载窗口。

于 2012-08-31T21:13:10.067 回答