tags:

views:

1235

answers:

3

Hi,

I have an addin which I want to invoke through Excel interop from a C# winforms application.

I can't get the addin etc. to load unless I uninstall and resinstall it each time (this is apparantly something to do with Excel not loading addins when you use interop - btw, can't get their example to work in C#). Unfortunately this is slow and annoying to the user so I need to streamline it.

I want to have one instance of Excel but load an already installed addin without forcing this install/reinstall problem.

I've searched and searched but everything I find on google gives the solution to install/reinstall. Is there any other way? The add-in is installed, I just want excel to load it.

This is what I am doing at the moment (taken from google'd advice):

                // loop over the add-ins and if you find it uninstall it.
                foreach (AddIn addIn in excel.AddIns)
                    if (addIn.Name.Contains("My Addin"))
                        addin.Installed = false;

                // install the addin
                var addin = excel.AddIns.Add("my_addin.xll", false);
                addin.Installed = true;
A: 

but only if I uninstall and resinstall it each time.

That doesn't seem right. You should be able to install it and have it stay there. How are you installing it?

Ray
It is there, it's just not loaded. Why? ask MS (http://support.microsoft.com/kb/213489), apparantly when using interop you're supposed to load the addins manually (to streamline interop performace) but how? That's what I want to know.
A: 

After a while I found the answer hidden in strange places in the MS help: and this blog post.

That isn't all the info you need though. Things to note: you must have at least one workbook open or otherwise Excel barfs. Here's some rudementry code to get started:

var excel = new Application();
var workbook = excel.workbooks.Add(Type.Missing);
excel.RegisterXLL(pathToXll);
excel.ShowExcel();

If you want you can close the temporary workbook (if you've run some macros etc.) and remember to tidy everything up with plenty of calls to Marshal.ReleaseComObject!

A: 

Thanks for the post. This saved some precious time!

Fayssal El Moufatich
@Fayssal: It's nice to say thanks, however you should'nt do this as an "answer". A reply to another answer should always be made as a comment, or you can vote up the answer. Now, your "answer" appears top-most right below the question which is a little bit confusing...
chiccodoro