none
Load and Unload Com addins RRS feed

  • Question

  • Hi,

    Is there a way to load and Unload addin dynamically after Excel is opened?

    I know there is comaddin.connect = false property but that wont unload the Ribbon UI..

    also when I call comaddin.connect = true it wont load the Ribbon UI and it just call ThisAddIn_Startup method but my addin logic is in RibbonNavigation_Load..

    What i am trying to achieve here is if user open Excel and say some startup variable is not available addin should not load and once those variable are available it should load in Excel without user interaction..Is it possible?

    I think i would like to mention, I have no control on addin I just want to unload it from external exe application.

    thanks

    Alkingson



    • Edited by Alkingson Tuesday, July 24, 2012 5:16 PM
    Tuesday, July 24, 2012 3:57 PM

Answers

  • I got my own answer, when I loop through Comaddin like below it creates new instance of Excel in taskmanager and won't load the Ribbon UI back on to user Excel..Maybe its doing it on the other instance of Excel which is invisible..

                        

                        foreach (COMAddIn c in (new Microsoft.Office.Interop.Excel.ApplicationClass()).COMAddIns)
                        {
                             if (c.Connect)
                    {
                        c.Connect = false;

                    }
                    c.Connect = true;
                        }

    so i did something like this

                Microsoft.Office.Interop.Excel._Application app = null;
                    app = Marshal.GetActiveObject("Excel.Application") as Microsoft.Office.Interop.Excel._Application;

             object index = "ExcelAddin3";
                                COMAddIn addin = null;
                                    addin = addins.Item(ref index);

       if (addin.Connect)
                                        {

                                        }
                                        else
                                        {
                                            addin.Connect = true;
                                        }

    This code reconnects the addin and bring back Ribbon UI..

    Thanks,

    Alkingson

    Wednesday, July 25, 2012 12:30 PM

All replies

  • Hi Alkingson,

    Here's an example of loading the ribbon that we use in a startup routine from VBA.  You might see if adapting this works for you. 

    Best regards,

    Anonymous9748

        Dim RibbonAddin As Office.COMAddIn
        Set RibbonAddin = GetRibbonAddin
        If Not RibbonAddin Is Nothing Then
            If RibbonAddin.Connect = False Then
                ' The ribbon was found, but not connected; try connecting
                RibbonAddin.Connect = True        ' This will throw an error if the Addin is disabled
            End If
            Exit Function
        End If
    Private Function GetRibbonAddin() As Office.COMAddIn
        Dim AddinItem As Office.COMAddIn
        ' Find the ribbon add-in
        For Each AddinItem In Application.COMAddIns
            If AddinItem.progid = RIBBON_PROGID Then
                Set GetRibbonAddin = AddinItem
                Exit Function
            End If
        Next
    End Function

    Tuesday, July 24, 2012 7:36 PM
  • Much easier would be to always load add-in and only use callbacks in ribbon xml to decide if your custom buttons should be shown or not
    Tuesday, July 24, 2012 8:33 PM
  • I dont have any control on addin..My program is external exe that will enable and disable addin depending on the some variable available or not..
    Tuesday, July 24, 2012 8:35 PM
  • Hi Alkingson

    This question comes up periodically. See this discussion, as a starting point:
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/9eb74bc6-7d29-4a1c-a860-35933486d500/

    also:
    http://social.msdn.microsoft.com/forums/en-us/vsto/thread/2BAF5293-8AA9-45B1-B68F-0BFD734BE462


    Cindy Meister, VSTO/Word MVP

    Wednesday, July 25, 2012 8:59 AM
    Moderator
  • Cindy,

    thank you for your reply..

    comaddins.connect = true does not reload Ribbon UI..it does call Addin_Startup method as i mentioned before..we are already doing same from my exe project as mentioned in above link..sorry that is not working for me..

    Wednesday, July 25, 2012 11:54 AM
  • I got my own answer, when I loop through Comaddin like below it creates new instance of Excel in taskmanager and won't load the Ribbon UI back on to user Excel..Maybe its doing it on the other instance of Excel which is invisible..

                        

                        foreach (COMAddIn c in (new Microsoft.Office.Interop.Excel.ApplicationClass()).COMAddIns)
                        {
                             if (c.Connect)
                    {
                        c.Connect = false;

                    }
                    c.Connect = true;
                        }

    so i did something like this

                Microsoft.Office.Interop.Excel._Application app = null;
                    app = Marshal.GetActiveObject("Excel.Application") as Microsoft.Office.Interop.Excel._Application;

             object index = "ExcelAddin3";
                                COMAddIn addin = null;
                                    addin = addins.Item(ref index);

       if (addin.Connect)
                                        {

                                        }
                                        else
                                        {
                                            addin.Connect = true;
                                        }

    This code reconnects the addin and bring back Ribbon UI..

    Thanks,

    Alkingson

    Wednesday, July 25, 2012 12:30 PM