Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations

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.

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?

# May 6, 2007 3:32 AM

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!

# May 6, 2007 3:37 AM

David Weinberg said:

I do not have the full VS, just the express editon. Is there are way to create a outlook add-in with VS Express?

# September 11, 2007 11:22 AM

Marc said:

I receive an error with this add in.

"Not loaded. The Managed Add-in Loader failed to initialize."

why is this?

# February 12, 2009 4:45 PM

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

# February 23, 2010 10:02 AM

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

# February 24, 2010 10:10 AM

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

# February 26, 2010 12:44 AM

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.

# April 6, 2011 1:25 AM

Motivation to lose weight said:

ELq8Lm I loved your article.

# April 8, 2012 12:59 PM

Eduard said:

It does not work on Win7 64bit using Outlook 2010 64bit

# September 19, 2012 7:04 AM