VSTO - Outlook - Unload addin

I have written an addin for Outlook 2003 using VS.net 2005, VSTO, and C#.

In "ThisApplication_Startup" I perform several tests to see if the addin should load for the current user. If the addin should not load, how do I unload the addin without shutting down outlook I keep seeing things that say you can shut down the addin's app domain, but I have not seen code that says how to get the addin's app domain. If this is how you are supposed to do it could someone give me a code snippet to accomplish this If there is a better way could someone please let me know what it is

Thank you very much

Kelly Johnson



Answer this question

VSTO - Outlook - Unload addin

  • PatrickEh

    This also doesn't seem to work. As soon as

    AppDomain.Unload(AppDomain.CurrentDomain)

    is hit, an exception is generated and the code falls through to my catch. The exception that is generated has no message or other info EXCEPT for the info that the exception occurred on the line above.

    Any ideas

    Thank you

    Kelly Johnson


  • Nathanial Woolls

    Thank you very much. That worked great.

    Thank you again

    Kelly Johnson


  • x_collins

    Hi Kelly,

    I understand that you are trying to unload the VSTO solution based of logic in the startup. Below is a code sample on how you could do this. This works for Word and Excel solutions. Note that doing this in Outlook will cause your add-in to be blacklisted. Outlook does this if the startup doesn't return correctly, for example if there was an exception thrown.

    Public Class ThisDocument

    Private Sub ThisDocument_Open() Handles Me.Open

    'see that the code is actually running

    MessageBox.Show("Opened")

    End Sub

    Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

    Dim I As String = InputBox("Shutdown, 'Doc' or 'Code'")

    If I = "Doc" Then

    'Close the document and don't save changes

    Me.Close(False)

    ElseIf I = "Code" Then

    'shutdown the code but open the document

    AppDomain.Unload(AppDomain.CurrentDomain)

    End If

    'If not 'Doc' or 'Code' then just open normally

    End Sub

    Private Sub ThisDocument_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub

    End Class

    Paul Stubbs
    Program Manager



  • Mircea Marghidanu

    Maybe this is a really dumb question, but how does this work

    When I tried this code, the compiler complained that 0x80004004 would not go into an int (it needed to be a uint). (0x80004004 = 2,147,500,036 an int can only hold 2,147,483,647). If I instead use

    const uint E_ABORT = 0x80004004;

    that part compiles fine, but "ThrowExceptionForHR" complains because it is expecting an int and not a uint.

    If I try:

    System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(0x80004004);

    the compiler also complains about a uint being passed in ThrowExceptionForHR's int parameter.

    Any light you could shine on this would be greatly greatly appreciated.

    Thank you very much

    Kelly Johnson


  • epion

    Sorry, I was a bit careless. Try declaring as below instead:

    const int E_ABORT = unchecked((int)0x80004004);

    Also notice that at debug time you will get a dialog saying the add-in failed to load. However, this dialog will not pop up if Outlook started normally i.e. not under debugger.



  • khs1

    If you want to tell Outlook to unload your add-in without disabling it you need to return E_ABORT. To do so from managed code you would to throw an exception with the corresponding code.

    Something like this should work:

    const int E_ABORT = 0x80004004;

    void ThisApplication_Startup()
    {

    ....... do your logic here
    if (needToUnload)
    System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(E_ABORT);
    }



  • Martin Selway

    For example, using VS.NET 2003 and C#, you could do the following to unload the addin programatically:

    Marshal.ReleaseComObject (m_AddInInstance);

    Marshal.ReleaseComObject (m_OutlookApplicationObject);

    GC.Collect();

    GC.WaitForPendingFinalizers();

    GC.Collect();

    GC.WaitForPendingFinalizers();

    Obviously that code doesn't work with VS.NET 2005, C#, and VSTO. But there has to be some way to programmatically unload an addin in its "ThisApplication_Startup" method. There are too many scenarios where this is important for it to have been left out (for example: licensing and error handling)

    Thank you

    Kelly Johnson


  • Christoffee

    Thank you both very much for the replies.

    Thanks again

    Kelly Johnson


  • helloworld123

    Hi Kelly,
    You can do something like this:

    int hr2 = unchecked ((int)0x80004004); // cast succeeds even if overflow
    System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr);

    Error HRESULTs are 32 bit values with the most significant bit set to 1 to indicate an error. E.g. 0x81234567

    Hex 8 0 0 0 4 0 0 4
    Bin 1000 0000 0000 0000 0100 0000 0000 0100

    But Int32.MaxValue is defined as 0x7fffffff:

    Hex 7 f f f f f f f
    Bin 0111 1111 1111 1111 1111 1111 1111 1111

    Notice that the M.S.B is reserved for the sign bit. We need to set this bit, but the value 0x80004004 can not be normally assigned to an Int32 type. The unchecked keyword ensures that no overflow exception is thrown even though the value is larger than Int32.MaxValue.



  • VSTO - Outlook - Unload addin