none
Problem with showing add-in functions in Excel RRS feed

  • Question

  • Hey,

    I am trying to create a add-in for Excel. After registration in Regedit, it is visible in the option window for Excel-add-in's.


    But when I want te use a function within this add-in, it is nowhere to be found.


    Also I noticed that the add-in is still inactive and nowhere to befound inside the Excel-addins from Regedit


    Is there anyone who can help me?

    Is is my code from the dll, Class Zoek:

    [ComVisible(true)]
        [Guid("17E5A96C-E727-40F9-A7C6-26CEC23F07F0")]
        //[ComSourceInterfaces(typeof(IZoek))]
        [ComDefaultInterface(typeof(IZoek))]
        [ClassInterface(ClassInterfaceType.None)]
    //    [ProgId("AgbExcel.Zorgverlener.Zoek")]
        public class Zoek : IZoek
        {
            private readonly AgbcodeServiceSoapClient _agbServiceClient;
            public Zoek()
            {
                _agbServiceClient = new AgbcodeServiceSoapClient();
            }
    
            #region Excel Functions
    
                    #endregion
    
    
            [ComRegisterFunctionAttribute]
            public static void RegisterFunction(Type type)
            {
                Registry.ClassesRoot.CreateSubKey(
                    GetSubKeyName(type, "Programmable"));
                RegistryKey key = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), true);
                key.SetValue("", System.Environment.SystemDirectory + @"\mscoree.dll", RegistryValueKind.String);
            }
    
            [ComUnregisterFunctionAttribute]
            public static void UnregisterFunction(Type type)
            {
    
                Registry.ClassesRoot.DeleteSubKey(
    
                    GetSubKeyName(type, "Programmable"), false);
            }
    
            private static string GetSubKeyName(Type type,
    
                string subKeyName)
            {
                System.Text.StringBuilder s = new System.Text.StringBuilder();
                s.Append(@"CLSID\{");
                s.Append(type.GUID.ToString().ToUpper());
                s.Append(@"}\");
                s.Append(subKeyName);
                return s.ToString();
    
            }
        }

    And My Interface IZoek:

    [ComVisible(true)]
        [Guid("8F879475-AA4A-473E-B094-5F2F47899D46")]
        public interface IZoek
        {
            #methods
        }


    Wednesday, December 6, 2017 2:48 PM

Answers

  • One alternative for building C# add-ins with UDF functions is the Excel-DNA library (that I develop).

    It's a completely free and open source library that uses the Excel C API (from the Excel SDK) to integrate with Excel, instead of using COM and making an "Automation Add-In".

    The easiest way to get started is to install the "ExcelDna.AddIn" Nuget package and follow the instructions from the Readme from there. Support for any issues your run into can be found on the Excel-DNA Google Group.

    -Govert

    Excel-DNA - Free and easy .NET for Excel

    • Marked as answer by lotje1990 Monday, December 11, 2017 7:59 AM
    Thursday, December 7, 2017 1:44 PM

All replies

  • Hello,

    According to your code, it seems that you create automation add-ins based on the method to register functions, but it seems that you create COM add-ins as you create a service object. 

    What add-in do you create? Do you want to call the method in your VBA macro or create UDF?  Please visit Excel COM add-ins and Automation add-ins for the differences. 

    If you want to call the function in the macros, you could create a COM add-in, create a service object and then expose the service through COMAddIn.Object property.

    If you want to create UDF, please create automation add-ins or automation add-ins that implement IDTExtensibility2

    To create a COM add-in, please visit  How to build an Office COM add-in by using Visual C# .NET

    To create automation add-ins with UDF, you could download the sample C# Excel Automation addin (CSExcelAutomationAddIn)

    Using C# .NET User Defined Functions (UDF) in Excel shows how to create UDF with automation add-ins that implement IDTExtensibility2. 

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 7, 2017 2:19 AM
    Moderator
  • I think I have to make a Com Add-in for Excel.

    But the link you send about building an Office COM add-in is not really clear for me. I still don't know how to build it.

    Do you maby have a other tutorial for me?

    Thursday, December 7, 2017 12:52 PM
  • One alternative for building C# add-ins with UDF functions is the Excel-DNA library (that I develop).

    It's a completely free and open source library that uses the Excel C API (from the Excel SDK) to integrate with Excel, instead of using COM and making an "Automation Add-In".

    The easiest way to get started is to install the "ExcelDna.AddIn" Nuget package and follow the instructions from the Readme from there. Support for any issues your run into can be found on the Excel-DNA Google Group.

    -Govert

    Excel-DNA - Free and easy .NET for Excel

    • Marked as answer by lotje1990 Monday, December 11, 2017 7:59 AM
    Thursday, December 7, 2017 1:44 PM
  • Hello,

    As you create a COM add-in and the add-in could be successfully loaded. 

    To expose the method, you could use: 

        public class AddInService : IAddInService
        {
    
            public AddInService()
            {
            }
            public void Hello()
            {
                MessageBox.Show("Hello");
            }
    
        }
        [ComVisible(true)]
        public interface IAddInService
        {
            void Hello();
        }
            public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
            {
                Excel.Application app = application as Excel.Application;
                IAddInService _service = new AddInService();
                ((COMAddIn)addInInst).Object = _service;
            }

    Then you use the following code to call the method:

    Sub callCOMMethod()
    Dim a As COMAddIn
    Dim b As Object
    Set a = Application.COMAddIns("ProgId")
    Set b = a.Object
    b.Hello
    End Sub

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 8, 2017 9:24 AM
    Moderator