Preventing Outlook from closing with Alt-F4

Tags: .NET, C#, CodeSnippets, Office

I have come to the realization, at some point, that I never want to close Outlook. Really, there's no reason for me to shut it down except when doing something special, like freeing every available byte of memory, or when developing add-ins to it. During day-to-day work, I always want it on and minimized. The problem is that there's no easy keyboard shortcut to minimize Outlook like there is to close it - I don't like the two-step shortcut of Alt+Space - n, and I don't want to define a new non-standard keyboard shortcut that I have to remember in addition to the standard Windows keys.

So instead, I've decided to go the route that RSSBandit and other programs did - intercept the Alt-F4 keypress, and minimize to the tray rather than closing. Whenever I do want to close Outlook, I can do it via the File->Exit option. It's rare enough not to bother me.
Writing this proved to be ridiculously easy when using VSTO 2005 SE and a library I had already developed a while ago to hook into the keyboard events.

The steps are simple:

1) Create the Outlook Add-in Project. I wrote this for Outlook 2007 - it should work the same for 2003:

2) Add a reference to the HookLibrary mentioned above.

3) Write the following code in the ThisAddin_Startup event:

private KeyboardHook altF4Hook;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
   altF4Hook = new HookLibrary.KeyboardHook();

   // false/true/false is for ctrl/alt/shift modifiers, respectively.
   altF4Hook.AddFilter(Keys.F4, false, true, false);
   altF4Hook.KeyPressed += new KeyboardHookEventHandler(altF4Hook_KeyPressed);
   altF4Hook.Install();
}

void altF4Hook_KeyPressed(object sender, KeyboardHookEventArgs e)
{
   Outlook.Explorer exp = Application.ActiveExplorer();
   exp.WindowState = Microsoft.Office.Interop.Outlook.OlWindowState.olMinimized;

}

 

And that's it. On our development machine it should automatically be installed in Outlook. For other machines, we can build the MSI file that's created for us and install it. I've attached the MSI here, as well as the full code. Enjoy.

10 Comments

  • Leonid (180) said

    Hi Avner, it was nice to see your posts and congrats on your new position :-). One comment though, since the hook mechanism is global to windows (as I vaguely remember :-)) don't you need to verify that the Outlook window is the active window (i.e. has the focus)? Otherwise, pressing Alt-F4 in other windows will minimize outlook? Or perhaps I don't have all the details and your Hook Library already handles that?

  • Avner Kashtan said

    First of all, thanks. Secondly - the hooking class I use (see the link above) uses thread-local hooks rather than global hooks, which are both problematic as you mentioned, and also much heavier on the computer having to intercept every single call. This way is easier and quicker. Yay!

  • Matt said

    When I attempt to activate the COM Add-Ins I have a message { Not loaded. The Managed Add-in Loader failed to initialize. } could you please give me some advice. Thank you Matt

  • Soeren said

    The dll works fine. However, I develope my Add-in using Visual Basic. I don't know how to translate the code above to VB. I will appreciate if somebody will do me that favor. Thank you Soeren

  • matt@yebo.co.za said

    Will not load as add-in in Outlook 2007. "Load Behavior: Not Loaded. The managed Add-in loader failed to initialize" please assist Matt

  • Matt said

    Great example thank you. How do you prevent Outlook from closing after it has been minimized? I'm running your code in Outlook 2007 with Visual Studio 2010 and Outlook closes after it is minimized.

Comments have been disabled for this content.