Preventing Outlook from closing with Alt-F4

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.