none
when to release outlook add-in object? RRS feed

  • Question

  • Hi team,

    I defined some MAPIFolder object at the class level, when should i release them?

        public partial class ThisAddIn
        {
            private MAPIFolder favouritesFolder = null;

         }

    Monday, March 24, 2014 8:21 AM

Answers

  • The VSTO Shutdown() event won't fire for Outlook 2010 or 2013, if that's what you're using. The Extensibility OnDisconnection() event is used to fire Shutdown() and OnDisconnection() was disabled in 2010/2013.

    What you can do is handle the Outlook Application.Quit() event, which will fire for you as the trigger to release any class or global level objects.

    See http://msdn.microsoft.com/en-us/library/ee720183(v=office.14).aspx for more information.


    Ken Slovak MVP - Outlook

    • Marked as answer by 董建华 Wednesday, March 26, 2014 3:23 AM
    Tuesday, March 25, 2014 2:11 PM

All replies

  • Hello,

    Release an Outlook object when you have finished using it. There is no strict rules where and when underlying COM objects should be released. Please see Systematically Releasing Objects for more information.

    You can use any Outlook or VSTO event handler (for example, the Shutdown event, finalizer).

    Finally, to prevent using a released variable, set it to null (Nothing in VB.NET).

    Monday, March 24, 2014 10:20 AM
  • Hi Eugene,

    I have noticed the shut down event, but it only worked when customer disable the add-in.

    When i closed the outlook manually, the shut down event does not work.

    Do i need finalizer to release the favouritesFolder?

    Tuesday, March 25, 2014 6:55 AM
  • The VSTO Shutdown() event won't fire for Outlook 2010 or 2013, if that's what you're using. The Extensibility OnDisconnection() event is used to fire Shutdown() and OnDisconnection() was disabled in 2010/2013.

    What you can do is handle the Outlook Application.Quit() event, which will fire for you as the trigger to release any class or global level objects.

    See http://msdn.microsoft.com/en-us/library/ee720183(v=office.14).aspx for more information.


    Ken Slovak MVP - Outlook

    • Marked as answer by 董建华 Wednesday, March 26, 2014 3:23 AM
    Tuesday, March 25, 2014 2:11 PM
  • As I already wrote, it is up to you where and when to release underlying COM objects. The best way is to release an Outlook object when you have finished using it. As Ken noticed, sometimes the shut down event may not work. Actually, it depends on the settings, whether the fast shut down feature of Outlook 2010 is enabled or not.

    You can check out the following registry keys:

    HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\Addins\<ProgID>\[RequireShutdownNotification]=dword:0x1

    HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\14.0\Outlook\Options\Shutdown\[AddinFastShutdownBehavior]=dword:0x1


    Setting AddinFastShutdownBehavior to 0x1 enables shutdown notifications for all add-ins. Setting the value to 0x0 uses the default behavior of Outlook 2010.
    Tuesday, March 25, 2014 2:37 PM
  • Hi Ken,


    The Application.quit() is worked well, thanks for your reply.

    Wednesday, March 26, 2014 3:18 AM
  • Hi Eugene,

    I could not find the register key [RequireShutdownNotification], [AddinFastShutdownBehavior] at my outlook environment.

    As we should not change the regedit, so i think the application.quite event is better.

    Thanks for you relply.


    • Edited by 董建华 Wednesday, March 26, 2014 3:22 AM
    Wednesday, March 26, 2014 3:22 AM
  • Please refer to the document which describes the fast shutdown feature.

    > As we should not change the regedit, so i think the application.quite event is better.

    Ken just noticed that Outlook introduced the fast shutdown feature you need to be aware of.

    Again, nobody can say that the Quit event handler is the right place for such things. It is up to you which event handler is to use.

    The best way is to release an Outlook object when you have finished using it, not keep it alive till the end of session.

    Wednesday, March 26, 2014 7:50 AM