2
votes

VSTO
VS2008 SP1
.NET 3.5
Excel 2007

I am a .net noob. I am trying to load an automation addin that is an excel application/automation addin (it is a dll not xla or xll) from within a vsto addin in the ThisAddIn_Startup() method of the vsto addin. From google I got the below solution which is not working.

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{

        Application excel = Globals.ThisAddIn.Application;
        //Also tried without display alerts being set to false
        excel.DisplayAlerts = false;
        foreach (AddIn addin in excel.AddIns)
        {
            if (addin.progID.Equals("MY_ADDIN_PROG_ID"))
            {
                Debug.WriteLine("Addin installed is " + addin.Installed);
                addin.Installed = false;
                Debug.WriteLine("Addin is: " + addin.FullName + ", " + addin.progID);
                Debug.WriteLine("Addin installed is " + addin.Installed);
            }
        }
        AddIn addIn = excel.AddIns.Add("MY_ADDIN_PROG_ID", false);
        addIn.Installed = true;
        excel.DisplayAlerts = true;
        Debug.WriteLine("Addin is: " + addIn.FullName + ", " + addIn.progID);
        Debug.WriteLine("Addin installed is " + addIn.Installed);
        excel.DisplayAlerts = false;
        //OTHER STARTUP CODE
        Debug.WriteLine("Starting up addin!");
}

Note, I can see the addin.installed is being set to false and back to true on startup but when I try to populate worksheet with udfs from the addin I tried to load in a later button_click method, I get #NAME? error. I am at my wits end. Any help will be greatly appreciated.

If I first try to call the udf in excel by typing it in a cell by hand before I call my button click method, the worksheet population works and the udfs get evaluted as expected but this is not ideal.

Also setting installed property to true does not seem to be doing anything as i can still see the udf addin as inactive in excel, it is only if I type it into a cell that it gets activated. Is there anything else I need to do to activate the automation addin in my vsto startup?

Thanks!

4

4 Answers

0
votes

I'm not sure you want to do this in the startup event. I have done something similar but not quite the same before which may be applicable. I exposed some COM visible functions to VBA in a different event handler:

protected override object RequestComAddInAutomationService()
{
    // return something com-visible
}

So maybe you can try to load your automation dll this way? This happens before the startup event fires... Excel might be doing something like locking its list of addins while a startup event is being handled - who knows? If it were possible to know Excel programming would be less tedious.

0
votes

It is harder than it seems to combine VSTO and Automation in Excel. You may find my blog post helpful:

Communicating Between VSTO and UDF's in Excel

0
votes

Just need to add String Value to the following registry key and you are good.

For Office 2007

Find regkey, HKEY_CURRENT_USER\SOftware\Microsoft\Office\12.0\Excel\Options, then create string value, where name = OPEN, value = /A "YOUR ADDIN NAME HERE" (quotes need to be included as well.)

Note that for the first addin, value name should be called OPEN, for the second one and onwards, use OPEN1, OPEN2, ... etc.

For Office 2010

Just replace 12.0 with 14.0 in the above regkey path, the rest are all the same.

Check out below article on MSDN, which will also help you a lot.

http://support.microsoft.com/kb/291392

0
votes

Looks like this is a bug specific to VSTO. I converted my addin to a COM addin and was able to use the automation addin from code after that. My team has sent the issue to microsoft so we'll see what they say.