1

After a few hrs of experimentation and google searches I have finally come to the end of what I can figure out on my own, so here is what I have now:

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"); } 

All of this works, and when I close outlook I see the explorer close event and quit event message boxes in order. However by this point outlook seems to already have closed, and I have no idea how to cancel these events (for some other events there is a bool Cancel passed in that you can set to false, but not for these events?), or send a minimize event (I haven't been able how to figure this out at all).

If anyone has any suggestions I'd really appreciate it. I had some spare time at work and figured I'd try to learn some addin dev stuff, and solve a really annoying part of outlook at the same time!

EDIT: I have also tried:

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

at startup to just immediately minimize the window. It does minimize outlook, but not to the systen tray, only onto the bar (which is funny, and probably actually a bug, since minimize is set to minimize to tray...) Still, if I could just get rid of the close/quit event(s) I could at least just minimize the window to the taskbar.

7
  • As far as I know, you cannot stop Outlook from closing in an AddIn. Commented Mar 5, 2012 at 20:31
  • If not in an Addin then how? Are there any resources you could point me too? Commented Mar 5, 2012 at 20:56
  • and am I just getting false hope by seeing the setting the return value of the method to false canceling the event from VB? msdn.microsoft.com/en-us/library/ff862184.aspx Commented Mar 5, 2012 at 20:58
  • "Represents the window in which the contents of a folder are displayed." Based on that, I would say that you cannot stop it from being closed. It appears you're looking to write an AddIn which keeps the (X) button from closing Outlook; instead minimizing it to the system tray? Commented Mar 5, 2012 at 21:08
  • Yes, that is exactly what I'm trying to do, however if it can't be done from an add-in, is there a better way to catch the close event to the window and replace it with a minimize event? I started looking at codeproject.com/Articles/24066/… but to be honest I've got no idea how that works, and would have to spend even more time getting up to speed on that, if you know of a good tutorial I'd really appreciate it though Commented Mar 5, 2012 at 21:12

1 Answer 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); } } 

It's not very clean, but this worked for me. It simply starts a new instance of Outlook when you close it and always minimizes Outlook on startup. Major drawback of course is that you can't really close Outlook anymore without first disabling the add-in or killing "outlook.exe" in your task manager. Not the best solution, but it does make sure that you'll never miss an email or calendar reminder because you accidently closed Outlook.

EDIT (10/02/12): I updated the restart outlook part of the code. It now starts outlook.exe with the /recycle switch. This attempts to reload Outlook in an existing window. It also restarts outlook using the minimized window style. This prevents outlook's loading window from appearing.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.