Posted on 2006-06-19
I have a need to programmatically quit Microsoft Outlook. I am writing the code in VB.NET; but I would not expect that the basics of the technique itself would not be language specific.
Assume that Microsoft Outlook is running. The following code does not work:
Private Sub QuitOutlook()
Dim oApp As Outlook.Application = New Outlook.Application
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
oApp = Nothing
The result of the above code is that Outlook is hidden but "Outlook.EXE" still appears in the "Windows Task Manager".
I *don't* want to "kill the process". Killing the Outlook process *does* remove "Outlook.EXE" from the Task Manager. However, if the user happens to have an email message *unsaved* draft that they are currently working on, all work is lost. I want the user to have the option of saving their changes. The "QuitOutlook() code snippet I provided in the above paragraph *will* prompt the user with the "Do you want to save changes?" warning message before the window goes away -- but the problem with QuitOutloook() is that Outlook.exe doesn't *completely* go away.
The addition of “oApp.Session.Logoff()” does not help.
The addition of “oNS.Logoff()” does not help.
I have also noticed that QuitOutlook() only *sometimes* works when running within the Visual Studio VB.NET environment (debugger), but *never* works when I “Rebuild the Solution” and run the project exe.
I don’t know whether the following information will help anyone determine what the problem is . . .
Even if I *manually* quit Outlook (select “Exit” from the Outlook’s “File” menu) after calling the QuitOutlook() code snippet, the “Outlook.EXE” still shows up in the Task Manager. And each time I manually launch Outlook the “Mem Usage” item in the Task Manager for Outlook.EXE continues to grow. This indicates some sort of memory leak. In this case the only way to get rid of “Outlook.EXE” from the Task Manager is to kill the process either programmatically or from within the Task Manager.
Does anyone have any ideas? It would seem to me that quitting an application would be easy!