In this post we are going to cover how to use an IDE (Integrated Development Environment) to write a DLL and use that DLL with Excel DNA. There are a few reasons we would pick this approach over just writing the code into a text/xml file.
- You get to use an IDE, and all the benefits that come with that…
- Complex code is easier to manage
- Debugging is much easier
- The DLL “protects” your IP (a bit)
- Working with userforms etc. becomes possible/much simpler
There’s other stuff as well but lets not labour the point.
OK, first which IDE to use? As far as I know all Microsoft IDE which support .Net 2 or above will work with the latest version of DNA. So that’s 2005 through to 2010, including any and all express versions. The great thing about DNA is its not compiling some oddball super specialized DLL which needs a lot of equally obscure registry settings and support files to run, its a bog standard managed DLL. But remember you will need the correct dot Net framework running on the target PC.
In this example I’m going to be using SharpDevelop [get it here]. It’s a free IDE for the dot Net framework (supporting, C#, F#, IronPython, and Boo). In many ways it’s not as polished the Express editions, but in some ways it’s better. The “Pro” editions of VS are in my opinion the finest development environments available, but they do cost a lot of money. The other monumentally good thing about SharpDevelop is that it installs and opens much quicker that any version of Visual Studio. Installing VS takes an age, opening VS 2008 (VB) express for the first time on my PC takes 30 seconds for SharpDevelop(3.2) its 4 seconds! Regardless of what you are using, the principals are the same, it might just be that the various bits are in slightly different places, just look around; you’ll find what you need in the end!
Ok, Step one (after installing the IDE, and downloading the DNA package*) is to create a new Class project. Open SharpDevelop, click File>New>>Solution:

In the dialog that pops up find the VB node, and select that (tip, you don’t need to drop into any of the child nodes), pick the “Class Library” project. Notice at the top right of the dialog there is a dropdown for picking the target framework version. Unless you have a compelling reason not too, pick “.Net Framework 2.0” – this should have the widest coverage. Finally fill out the details at the bottom and create the project.

In case you have not used an IDE before it worth us talking about what you have just done. In the world of Excel we normally create one file and that holds all our code. This is a little different, but don’t worry it’s not rocket sugary. What the IDE has just done is write to disk a few files that it will use to hold and manage the project, if you add new files, and when you build the project these file will be placed in this project folder. To start with there are 3 files and 3 sub folders. The folders are (briefly!); “Bin”, with another sub folder, this is where the output DLL will normally be written to, “Obj” with further sub folders; this is where the IDE puts the files it uses for building your project, and “Properties”, guess what goes in there! The 3 files are “XYZ”.sln, this is the solution file – this opens the project and all the other support things for this solution (for example if you had more than one project in a solution), “ZYX.vbproj”, the VB project you just created, this will also open the project, and the solution, but not any other projects in the solution, and “NewClass.vb”, this is the actual code file, like a module in VBA.
Ok, back in the IDE, you should have something that looks like this:

The first thing we need to do is add a reference to the Excel DNA integration assembly. To do this go to the Project menu, then “Add Reference”…

In the dialog that is displayed, go to the “.Net Assembly Browser” tab, and click Browse, then find your DNA folder, open the “Distribution” sub folder and select the file called “ExcelDna.Integration.dll”:

Click “Open” and the file will be added to the list of References, then click OK

Next we need to add some code to the class. At the top of the class file remove the comments and add “Imports ExcelDna.Integration”, while we’re here let’s rename the class to “Public Class MyFirstDNAAddin”. Next, in the project explore to the left of the screen, right click on the class file, pick rename, and change the name of the class file from “NewClass.vb” to something like “MyAddin.vb”

We are now ready to add some functional code. Lets Write a “Hello world” function. Add this code under the Class declaration:
Public shared Function HelloWorld (byVal sString As String) As String
Return "Hello World, from a DLL..." & sString
End Function
Next compile the project, hit F8, or Build>>Build Solution, or click one of the green down arrow buttons, shown in the red box below:

Cool, all being well, you should have just built a DLL. Notice at the bottom of the IDE there’s an “Output” window, which tells you how long the project took to compile, if there were any errors these should also be displayed at the bottom of the IDE.
Next go to your project Folder, and open the Bin folder and then the Debug folder, the DLL will be in here with two other files, the DNA integration DLL and a temp file, copy the DLL you just created – “MyFirstIDEAddin.dll”, or whatever you called it, in to a new folder,… somewhere new…

Next copy the “ExcelDNA.xll” from the DNA package (its in the distribution folder) into the same folder where your DLL now is. Then create a new text file and add the following code in it:
<DnaLibrary>
<ExternalLibrary Path=”MyFirstIDEAddIn.dll” />
</DnaLibrary>
Close the text file, saving the changes, and rename the text file to something like “HelloIDE.DNA”, finally change the name of the XLL to “HelloIDE.xll”, and double click it.

Excel should open, enable macros if needed, and open anew workbook. If you now go to the function wizard, your function should be in there, and you can use it as you would any other function.

Remember you need to distribute the three files for this to work on another PC, that’s the DNA file, the DLL file and the XLL. If you only want to send out 1 file you can use the packager, we will cover this in another post.
* download Excel DNA from codeplex, unzip the files, and copy them to some suitable location, “C:\ExcelDNA\\ExcelDna-0.27”, for example would be a good place.