Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Quit Outlook

Posted on 2006-06-19
5
1,098 Views
Last Modified: 2012-08-14
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.Quit()
            oApp = Nothing
      End Sub

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!


0
Comment
Question by:richelieu7777
  • 2
  • 2
5 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 16934309
1) Add Imports line
 
   Imports System.Runtime.InteropServices

2) Add a marshal call to release resources

   oApp.Quit()
   Marshal.ReleaseComObject(oApp)

3) Force garbage collection

   GC.Collect()

Bob
0
 

Author Comment

by:richelieu7777
ID: 16934463
Do I put the Marshal.ReleaseComObject(oApp) and GC.Collect() right after the "oApp.Quit" or after the "oApp = Nothing"?  I just want to clarify.
0
 

Author Comment

by:richelieu7777
ID: 16934475
Or do I put the Marshal.ReleaseComObject(oApp) and GC.Collect() *in lieu of* the oApp = Nothing and delete "oApp = Nothing"
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16934546
With Marshal.ReleaseComObject and garbage collection, you don't need to set the oApp to Nothing.  If you leave it in, it won't hurt anything, unless you put it before ReleaseComObject, or you will get an exception.

Bob
0
 

Expert Comment

by:epiphonedot
ID: 23755606
I'm building a COM dll in .NET that accesses Outlook for contacts and am trying to use this method to close Outlook, but it does not work. In testing, I took out all references except for an instance of the  Outlook Application. Is there something additional I need to know when using this method to close Outlook while using it in a class as opposed to an executable perhaps?
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question