This library implements helper functions to assist and modify the Excel-DNA function registration, by applying various transformations before the functions are registered.
The following transformations have been implemented:
Generation of wrapper functions for:
- Functions returning Task or IObservable as asynchronous or RTD-based functions (including F# Async functions)
- Optional parameters (with default values), 'params' parameters and Nullable parameters
- Range parameters in Visual Basic functions
Examples of general function transformations:
- Logging / Caching / Timing handlers
- Suppress in Function Arguments dialog
If you've previously used the CustomRegistration library, note that I've renamed and rearranged the project source, and renamed the output assembly from ExcelDna.CustomRegistration to ExcelDna.Registration. The last state of the project before the large-scale rearrangement is marked by the git tag CustomRegistration_Before_Rename, and can be retrieved from the release tab on GitHub.
To make a simple add-in that uses the Excel-DNA Registration extension to dynamically update the HelpTopic information for function registrations:
- Create a new C# Class Library (.NET Framework) project, e.g. called RegistrationHelpUpdate.
- Open the Package Manager Console.
PM> Install-Package ExcelDna.AddInPM> Install-Package ExcelDna.Registration- Edit the RegistrationHelpUpdate-AddIn.dna file to add the ExplicitRegistration flag to the function library, and add the reference to the
ExcelDna.Registrationfor packing:
<DnaLibraryName="RegistrationHelpUpdate Add-In"RuntimeVersion="v4.0">
<ExternalLibraryPath="RegistrationHelpUpdate.dll"ExplicitExports="false"ExplicitRegistration="true"LoadFromBytes="true"Pack="true" />
<ReferencePath="ExcelDna.Registration.dll"Pack="true" />
</DnaLibrary>- Insert the following code:
usingSystem.Linq;usingExcelDna.Integration;usingExcelDna.Registration;namespaceRegistrationHelpUpdate{publicclassAddIn:IExcelAddIn{publicvoidAutoOpen(){RegisterFunctions();}publicvoidAutoClose(){}publicvoidRegisterFunctions(){// There are various options for wrapping and transforming your functions// See the Source\Samples\Registration.Sample project for a comprehensive example// Here we just change the attribute before registering the functionsExcelRegistration.GetExcelFunctions().Select(UpdateHelpTopic).RegisterFunctions();}publicExcelFunctionRegistrationUpdateHelpTopic(ExcelFunctionRegistrationfuncReg){funcReg.FunctionAttribute.HelpTopic="http://www.bing.com";returnfuncReg;}}publicclassFunctions{[ExcelFunction(HelpTopic="http://www.google.com")]publicstaticobjectSayHello(){return"Hello!!!";}}}- Press F5 to compile and start in Excel.
- Start typing
=SayHello(in a cell and press the Fx button to open the function wizard. Check that the HelpTopic has been updated during registration to open Bing instead of Google.
See the add-ins in the Samples directory to see various registration update extensions.
Once you have a basic Visual Basic add-in working.
From the NuGet Package Manager Console, (or the Manage NuGet Packages dialog): PM> Install-Package ExcelDna.Registration.VisualBasic
Fix up your .dna file by changing to ExplicitRegistration for your library registration, and packing the extra ExcelDna.Registration libraries:
<DnaLibraryName="MyVisualBasic Add-In"RuntimeVersion="v4.0" > <ExternalLibraryPath="MyVisualBasic.dll"ExplicitRegistration="true"LoadFromBytes="true"Pack="true" /> <ReferencePath="ExcelDna.Registration.dll"Pack="true" /> <ReferencePath="ExcelDna.Registration.VisualBasic.dll"Pack="true" /> </DnaLibrary> 3.Perform the explicit registration in your AutoOpen by calling ExcelDna.Registration.VisualBasic.PerformDefaultRegistration():
ImportsExcelDna.IntegrationImportsExcelDna.Registration.VisualBasicPublicClassMyAddInImplementsIExcelAddInPublicSubAutoOpen()ImplementsIExcelAddIn.AutoOpen' Code here will run eery time the add-in is loaded PerformDefaultRegistration()EndSubPublicSubAutoClose()ImplementsIExcelAddIn.AutoClose' Code in here will run when the add-in is removed in the Add-Ins dialog, ' but not when Excel closes normally EndSubEndClassPublicFunctiondnaTestParams(date1AsDate,ParamArrays()AsString)AsStringReturns.LengthEndFunctionPublicFunctiondnaTestOptional(date1AsDate,OptionalheadAsBoolean=True)AsStringReturnhead.ToString()EndFunctionSample projects for this library can be found in the Excel-DNA Samples repository.
If you receive this error when opening your Excel addin, you need to add ExplicitRegistration="true" to the <ExternalLibrary Path="MyAddin.dll"... command in your .dna file.