[ACCEPTED]-Loading addins when Excel is instantiated programmatically-excel-addins

Accepted answer
Score: 31

I looked into this problem again, and the 8 Application.Addins collection seems to have 7 all the addins listed in the Tools->Addins 6 menu, with a boolean value stating whether 5 or not an addin is installed. So what seems 4 to work for me now is to loop through all 3 addins and if .Installed = true then I set 2 .Installed to False and back to True, and 1 that seems to properly load my addins.

Function ReloadXLAddins(TheXLApp As Excel.Application) As Boolean

    Dim CurrAddin As Excel.AddIn

    For Each CurrAddin In TheXLApp.AddIns
        If CurrAddin.Installed Then
            CurrAddin.Installed = False
            CurrAddin.Installed = True
        End If
    Next CurrAddin

End Function
Score: 6

Using CreateObject("Excel.Application") would have the same result as using 3 New Excel.Application, unfortunately.

You will have to load the 2 Addins that you need individually by file 1 path & name using the Application.Addins.Add(string fileName) method.

Score: 3

I'm leaving this answer here for anyone 20 else who ran into this problem, but using 19 JavaScript.

A little background... In my 18 company we have a 3rd party web app that 17 used JavaScript to launch Excel and generate 16 a spreadsheet on the fly. We also have an 15 Excel add-in that overrides the behavior 14 of the Save button. The add-in gives you 13 the option of saving the file locally or 12 in our online document management system.

After 11 we upgraded to Windows 7 and Office 2010, we 10 noticed a problem with our spreadsheet-generating 9 web app. When JavaScript generated a spreadsheet 8 in Excel, suddenly the Save button no longer 7 worked. You would click save and nothing 6 happened.

Using the other answers here I 5 was able to construct a solution in JavaScript. Essentially 4 we would create the Excel Application object 3 in memory, then reload a specific add-in 2 to get our save button behavior back. Here's 1 a simplified version of our fix:

function GenerateSpreadsheet()
{
    var ExcelApp = getExcel();
    if (ExcelApp == null){ return; }

    reloadAddIn(ExcelApp);

    ExcelApp.WorkBooks.Add;
    ExcelApp.Visible = true;
    sheet = ExcelApp.ActiveSheet;

    var now = new Date();
    ExcelApp.Cells(1,1).value = 'This is an auto-generated spreadsheet, created using Javascript and ActiveX in Internet Explorer';

    ExcelApp.ActiveSheet.Columns("A:IV").EntireColumn.AutoFit; 
    ExcelApp.ActiveSheet.Rows("1:65536").EntireRow.AutoFit;
    ExcelApp.ActiveSheet.Range("A1").Select;

    ExcelApp = null;
}

function getExcel() {
   try {
       return new ActiveXObject("Excel.Application");
   } catch(e) {
       alert("Unable to open Excel. Please check your security settings.");
       return null;
   }
}

function reloadAddIn(ExcelApp) {
    // Fixes problem with save button not working in Excel,
    // by reloading the add-in responsible for the custom save button behavior
    try {
        ExcelApp.AddIns2.Item("AddInName").Installed = false;
        ExcelApp.AddIns2.Item("AddInName").Installed = true;
    } catch (e) { }
}

More Related questions