Excel-DNA - Step-By-Step C# Add-In
Excel-DNA - Step-By-Step C# Add-In
Excel-DNA - Step-By-Step C# Add-In
A packing feature allows the Excel-DNA runtime library, configuration files, user
add-in assemblies and other dependent libraries to be packed into a single .xll
file. The only external requirement for using such an add-in would be that the
appropriate .NET runtime version should be installed.
Step-by-step instructions
Getting and testing Excel-DNA
In this section we download Excel-DNA and check that the simplest sample addin works. This ensures that the .NET framework is correctly registered, that the
Excel security settings will not prevent the add-ins from loading.
1. Download the latest version of Excel-DNA from
http://exceldna.codeplex.com and extract the .zip file to a convenient
location, say C:\Test\ExcelDna\.
2. Extracting the distribution will create folders called Distribution and
Source.
3. In the Distribution folder, double-click the ExcelDna.xll file Excel should
open and possibly display a security warning:
6. The code creating defining this add-in can be found in the ExcelDna.dna
file:
Troubleshooting
The following are some common problems which may arise when loading the
simplest samples add-in:
This may occur if the 64-bit version of Excel 2010 in installed. Load the
corresponding ExcelDna64.xll file instead.
The .NET runtime is not present.
Security restrictions prevent Excel from loading the file. The following
setting would cause Excel to silently disable all add-ins:
4. Add a file to the project called FirstAddIn.dna with the following content:
<DnaLibrary Name="First Add-In" RuntimeVersion="v4.0">
<ExternalLibrary Path="MyLibrary.dll" />
</DnaLibrary>
5. In the properties for this file, set that this file will be copied to the Output
directory:
6. Make a copy of the ExcelDna.xll into the project directory, and rename it to
FirstAddIn.xll:
7. Add this file to the project, and in the properties for the file ensure that it
will be copied to the Output directory:
8. Finally, build the project. The Output directory should look like this:
(Office14 is the directory for Office 2010, substitute Office12 for Office
2007, or another version as appropriate.)
2. Set a breakpoint in the MyFirstFunction function:
3. Press F5 to start debugging Excel should start and load the add-in. Add a
new sheet be selecting File->New or pressing Ctrl+N.
4. Enter the function in Excel and wait for the breakpoint to be hit:
Further development
Some directions for further development of the add-in:
Accessing the C API and the COM object model from Excel-DNA.
Multithreading functions, volatile functions, reference parameters.
Macros, menus, ribbons and custom task panes.
Registration-free RTD servers.