none
Add reference to Excel at run time using C# RRS feed

  • Question

  • Hi,

    I have and Excel xla file, bonds.xla, which calls a type library, written by c++, bondRisk.dll.  The bonds.xla was password protected. I want to write an vsto installation package for it. I am not able to modify the bonds.xla file. Can anybody teach me how to add bondRisk.dll to the Excel reference at run time using C#. 

    Thanks

    Here is my code 

    try{

       string directory=@"XXXX";

     var app = Globals.ThisAddIn.Application;
                    Excel.Workbook wb;
                    if (app.Workbooks.Count == 0) wb= app.Workbooks.Add();

                

                    

                    foreach (Excel.AddIn addIn in app.AddIns)

                        if (addIn.Name.Contains("Bonds"))
                            addIn.Installed = false;
                
                    var filePath= directory+"Bonds.xla";

                    MessageBox.Show(filePath);
                    var addin = app.AddIns.Add(filePath, true);
                    addin.Installed = true;
            
                    
                 

                }
                catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);

                    }

            }


    • Edited by yuxuzi Friday, August 1, 2014 4:06 PM
    Friday, August 1, 2014 4:06 PM

Answers

  • Hi yuxuzi

    If I understand you correctly, you want to add a Reference to the dll in the xla file? If this were being done manually that would mean opening the file in the VBA Editor, entering the password, then using Tools/References? Is that correct?

    If that is the case then there is no way your code can do this in any way other than mimicking the steps I list above, which means you would need to alter the xla file and you would have to have the password.

    Beyond that, your only hope would be that the person who maintains the xla file changes the code to use late binding without a direct reference to the dll.


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, August 3, 2014 7:43 AM
    Moderator