More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

forum 21-Jan-2017 10:08

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

    Page [ 1 2 Next >> ]  
 Outlook and .NET Printer Friendly Version
Writing code with C# and VB.NET to create Outlook add-ins and other projects
Topic
Outlook Hangs Upon Exit, Got it to Work - Good Article
I was having difficulty in getting Outlook 2003 to shutdown whenever I would exit it after my Addin had created an object. This article is priceless and now my program works great. I hope this can help others:

http://blogs.msdn.com/eric_carter/archive/2004/10/10/240568.aspx

I translated it to VB .Net and created a new Class "EventListener.vb":

Imports System
Imports Outlook

Public Class EventListener

    Public Delegate Sub ShutDown()

    Private application As Outlook.Application
    Private explorers As Outlook.Explorers
    Private inspectors As Outlook.Inspectors
    Private eventSinks As System.Collections.ArrayList
    Private shutdownHandlerDelegate As ShutDown

    Public Sub EventListener(ByRef application As Outlook.Application, ByVal shutdownHandlerDelegate As ShutDown)
        Me.application = application
        Me.shutdownHandlerDelegate = shutdownHandlerDelegate
        explorers = application.Explorers
        inspectors = application.Inspectors
        eventSinks = New System.Collections.ArrayList

        'AddHandler explorers.NewExplorer, AddressOf Explorers_NewExplorer(Outlook.Explorer)
        'AddHandler inspectors.NewInspector, AddressOf Inspectors_NewInspector(Outlook.Inspector)

        Dim e As Outlook.Explorer
        For Each e In application.Explorers
            Explorers_NewExplorer(e)
        Next

        Dim i As Outlook.Inspector
        For Each i In application.Inspectors
            Inspectors_NewInspector(i)
        Next
    End Sub

    Public Sub Explorers_NewExplorer(ByRef explorer As Outlook.Explorer)
        eventSinks.Add(explorer)
        Dim explorerEvents As Outlook.ExplorerEvents_Event = explorer
        AddHandler explorerEvents.Close, AddressOf Explorer_Close
    End Sub

    Public Sub Inspectors_NewInspector(ByRef inspector As Outlook.Inspector)
        eventSinks.Add(inspector)
        Dim inspectorEvents As Outlook.ExplorerEvents_Event = inspector
        AddHandler inspectorEvents.Close, AddressOf Inspector_Close
    End Sub

    Public Sub Explorer_Close()
        If (application.Explorers.Count <= 1 And application.Inspectors.Count = 0) Then
            HandleShutdown()
        End If
    End Sub

    Public Sub Inspector_Close()
        If (application.Explorers.Count = 0 And application.Inspectors.Count <= 1) Then
            HandleShutdown()
        End If
    End Sub

    Private Sub HandleShutdown()
        ' Release any outlook objects this class is holding
        application = Nothing
        explorers = Nothing
        inspectors = Nothing
        eventSinks.Clear()
        eventSinks = Nothing

        ' Force a garbage collection
        GC.Collect()
        GC.WaitForPendingFinalizers()

        ' Call client provided shutdown handler delegate
        shutdownHandlerDelegate()
    End Sub

End Class


I declare a global variable:

    Public Listener As EventListener ' Outlook event listener so shutdown is possible


I instantiate it in "OnStartupComplete":

       ' Create EventListener and associate ShutDown routine
        Listener = New EventListener
        Dim ShutItDown As EventListener.ShutDown
        ShutItDown = AddressOf ShutDown
        Listener.EventListener(applicationObject, ShutItDown)


This is my "Shutdown" module:

   ' Shutdown the Outlook Addin COM objects
    Public Sub ShutDown()
        Try
            Do
                If Marshal.ReleaseComObject(applicationObject) <= 0 Then Exit Do
                applicationObject = Nothing
            Loop
        Catch ex As System.Exception
            MsgBox("ERROR - " & ex.Message & " [ShutDown]")
        End Try
    End Sub
 

  07-Jan-2005  13:40
  07-Jan-2005  16:15   
Cool! What did you think of Eric's follow-up comment at http://blogs.msdn.com/eric_carter/archive/2004/10/20/245386.aspx ?
  11-Jan-2005  11:32   
Sue,

          I agree with Eric on using ReleaseCOMObject sparingly. In my case I had to use it in my Shutdown routine because it worked every time in releasing any objects attached to "applicationObject" and since it is the last thing I will do I no longer need "applicationObject" for connectivity.

          I was pulling my hair out until I found this article.
  02-Mar-2005  15:24   
Mark,
Your post is very valuble to me because I was stuck with Outlook not properly shuting down problem too. But when I added the EventListener.vp into my project, there were two errors because I turned on OPTION STRICT.

One is
Dim explorerEvents As Outlook.ExplorerEvents_Event = explorer
where convert explorer object to an event is not allow.

Two
Dim inspectorEvents As Outlook.ExplorerEvents_Event = inspector
Similar to "One", convert Outlook.Inspector to an event is not allow.

Can you give me a hint on this?
 
  04-Mar-2005  18:54   
I believe you have to declare them "WithEvents" like this:

    Private WithEvents explorers As Outlook.Explorers
    Private WithEvents inspectors As Outlook.Inspectors

I am using Outlook 2003 and it shuts down perfectly every single time.
  14-Apr-2005  03:20   
And how do I have to use those declarations?
  14-Apr-2005  08:32   
Anonymous, Mark's declarations declare explorers and inspectors collections that will raise the NewExplorer and NewInspector events that are crucial to knowing when Outlook shuts down -- as in the original code sample above. Use those declarations instead of these:

    Private explorers As Outlook.Explorers
    Private inspectors As Outlook.Inspectors
  04-Oct-2005  07:12   
Another Good article related to Outlook 2003 shutdown woes can be foun at http://www.shahine.com/omar/WhenReleaseComObjectIsNecessaryInOutlook.aspx.
Happy Coding....
  19-Oct-2005  21:14   
I have been reluctant using this article as I was determined to figure out this problem by myself. I have an add-in created in VB.NET that needed to create a backgroubd thread to which an outlook.application and other outlook object were used to do some work, but in vain, after many hours (and hours...), I have failed to fix the problem and therefore gave in and used the article and within minutes, the problem was fixed. So hats off for writting it and publishing it. Fantastic code and life saver!!!
  23-Jun-2006  15:08   
I have an outlook add-in written in C# which is hanging on shutdown. It seems to be the same problem many people are having. When I close outlook, it hangs with an icon in the system tray for about 5 secons. I did some debugging, and it is calling both the OnBeginShutdown and OnDisconnect methods, but it hangs before it calls them. I tried both of these suggested solutions,

http://blogs.msdn.com/eric_carter/archive/2004/10/10/240568.aspx
http://www.shahine.com/omar/WhenReleaseComObjectIsNecessaryInOutlook.aspx

And neither seem to fix the problem. I have an explorer close event which calls a method that sets all my Outlook Object Model objects to null and performs a garbage collection. Does anyone have any suggestions? When I uninstall my add-in, Outlook closes immediately. Thanks.

 
  22-Oct-2006  23:03   
In regards to the original post, what is the c# equivalent of:

ShutItDown = AddressOf ShutDown
    Page [ 1 2 Next >> ]