Search code examples
c#dllexcel-dna

Excel-Dna: Co-existence of ExcelCommand and ExcelDNA.Registration Package


I would like to write the ExcelCommand UDF in the C# program that uses the ExcelDNA Registration package.

However, in order to use the Registration package, if I set explicitRegistraiton="true" in the dna file, the following simple Excel command cannot be loaded. In fact, it is possible to compile, but the UDF command doesn't appear in the Excel add-in tab.

How can I use excel command in a program that uses ExcelDna registration?

// C# program
    [ExcelCommand(MenuName ="Test", MenuText ="Test")]
    public static void SayHelloCommand()
    {   
        MessageBox.Show("Hello");
    }

// .dna file
    <DnaLibrary Name="Registration.Sample Add-In" RuntimeVersion="v4.0" >
      <ExternalLibrary Path="Registration.Sample.dll" ExplicitRegistration="true" LoadFromBytes="true" Pack="true" />
      <Reference Path="ExcelDna.Registration.dll" Pack="true" />
    </DnaLibrary>

Solution

  • Once you set ExplicitRegistration to true, Excel-DNA expects you to register functions and commands yourself.

    The same way that you call ExcelRegistration.GetExcelFunctions().RegisterFunctions() to register functions, you can also call ExcelRegistration.GetExcelCommands().RegisterCommands() to register commands.

    e.g.

    public class AddIn : IExcelAddIn
    {
        public void AutoOpen()
        {
            ExcelRegistration
                .GetExcelCommands()
                .RegisterCommands();
    
            // ...
        }
    
        public void AutoClose()
        {
            // ...
        }
    }