More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

forum 23-May-2019 09:58

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
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:

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

        Dim i As Outlook.Inspector
        For Each i In application.Inspectors
    End Sub

    Public Sub Explorers_NewExplorer(ByRef explorer As Outlook.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)
        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
        End If
    End Sub

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

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

        ' Force a garbage collection

        ' Call client provided shutdown handler delegate
    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()
                If Marshal.ReleaseComObject(applicationObject) <= 0 Then Exit Do
                applicationObject = Nothing
        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 ?
  11-Jan-2005  11:32   

          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   
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.

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
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,

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 >> ]