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)"

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.