Add method of AddIns class failed trying to install AddIn

Mar 20, 2013 at 3:26 PM
Edited Mar 20, 2013 at 3:27 PM
Hi,
I'm trying to install an Addin using this code:
public void AutoOpen() 
 { 
        string xllPath = (string)XlCall.Excel(XlCall.xlGetName); 
        var xlApp = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application; 
        xlApp.AddIns.Add(xllPath, false /*don't copy file*/).Installed = true; 
    } 
But I get "Add method of AddIns class failed" error.

Anyone have had the same error? how did you solve it?

Best regards,

Jorge.
Coordinator
Mar 20, 2013 at 9:38 PM
Hi Jorge,

It seems there's a trick. You have to make sure that there is a visible workbook, before accessing the AddIns collection. See: http://support.microsoft.com/kb/280290

(But be careful - installing the add-in will causes AutoOpen to be called again.)

Your code might be:
    public void AutoOpen() 
    { 
        string xllPath = (string)XlCall.Excel(XlCall.xlGetName); 
        var xlApp = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application; 
        if (xlApp.Workbooks.Count == 0 ) xlApp.Workbooks.Add();
        xlApp.AddIns.Add(xllPath, false /*don't copy file*/).Installed = true; 
    } 
Regards,
Govert
Mar 21, 2013 at 11:55 AM
Hi Again,

After fix this problem we get another really weird.
Build solution, result:

CodeContracts (folder)
Images(folder, just an image)
MyExcel.ddl
MyExcel-Addin.dna
MyExcel-Addin.xll (a renamed copy of ExcelDna.xll from the distribution)
MyExcel-Addin-packed.xll


If I tried to run MyExcel-Addin-packed.xll I get a excel exception and addin is not installed but if I tried to run MyExcel-Addin.xll directly addin is installed properly.

Regards,

Jorge.
Mar 21, 2013 at 1:37 PM
Edited Mar 21, 2013 at 2:05 PM
Ok, got it working, the problem was the name of the packed xll, I just renamed it to match the original xll and it works as expected. Maybe it has something to do with the code I use to install the addin:
public void AutoOpen()
        {
            const string name = "BmfExcel-AddIn.xll";

            var xlApp = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application;
            var xllPath = (string)XlCall.Excel(XlCall.xlGetName);

            Workbook wb = null;
             
            var registered = false;
            
            foreach (object addIn in xlApp.AddIns)
            {
                if (((dynamic)addIn).Name == name)
                {
                    registered = true;
                }
            }
            if (!registered)
            {
                if (xlApp.Workbooks.Count == 0)
                    wb = xlApp.Workbooks.Add();
                xlApp.AddIns.Add(xllPath, false).Installed = true;

                if (wb != null)
                    wb.Close();
            }
            
        } 
Thanks for your help.