0

I am trying to create a custom Excel function in C# (I hope to pull report data from a web API into Excel). However when I go into Excel, I can add the new Automation Addin, but it is listed under "Inactive Application Add-ins", what's going wrong here?

I have created a COM automation addin based on this blog post. And my source code is on Github here.

I am working on a Window 7 64-bit machine with Visual Studio 2010 and Excel 2010 64-bit (version 14.0). My project is set to x64 build configuration, using .NET Framework 4.

I am using an interface:

namespace JS.Formulas
{
    public interface IFunctions
    {
        double JsAdd(double a, double b);
    }
}

And the class:

using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace JS.Formulas
{
    [ComDefaultInterface(typeof(IFunctions))]
    [Guid("652496B2-6F14-461B-98AF-D86B1BD5F439")]
    public class Functions : IFunctions
    {
        [ComRegisterFunction]
        public static void RegisterFunction(Type type)
        {
            string subkey = @"CLSID\{" + type.GUID.ToString().ToUpper() + @"}\Programmable";
            Registry.ClassesRoot.CreateSubKey(subkey);

            var key = Registry.ClassesRoot.OpenSubKey(@"CLSID\{" + type.GUID.ToString().ToUpper() + @"}\InprocServer32", true);
            if (key != null)
            {
                key.SetValue("", String.Format("{0}\\mscoree.dll", Environment.SystemDirectory), RegistryValueKind.String);
            }
        }

        [ComUnregisterFunction]
        public static void UnregisterFunction(Type type)
        {
            string subkey = @"CLSID\{" + type.GUID.ToString().ToUpper() + @"}\Programmable";
            Registry.ClassesRoot.DeleteSubKey(subkey, false);
        }

        public double JsAdd(double a, double b)
        {
            return a + b;
        }
    }
}

I have set up a post-build event to register the component using the 64-bit version of regasm like this: "%Windir%\Microsoft.NET\Framework64\v4.0.30319\regasm" "$(TargetPath)"

Jay Walker
  • 4,654
  • 5
  • 47
  • 53
Johnathan Sewell
  • 739
  • 10
  • 26

1 Answers1

3

I've decided to use the Excel DNA project for this instead. It took 5 minutes to get my custom function working, clear documentation too (so refreshing after looking through MSDN docs).

Johnathan Sewell
  • 739
  • 10
  • 26