Build Excel Automation Add-in in C#, C++, VB.NET
Excel user-defined functions (UDF) in Visual Studio .NET

Add-in Express™
for Microsoft® Office and .net

Add-in Express Home > Add-in Express for Office and .NET > Online Guide > Excel Automation add-ins

Excel Automation add-ins

Building Excel add-in / plug-in in Visual Studio .NET - Flash video In fact, the Excel Automation add-in (user-defined function, or UDF) does not differ from a COM add-in except for the registration in the registry. That's why Add-in Express bases Excel Automation Add-in projects on Add-in Express COM Add-in projects.

Add-in Express allows creating Automation add-ins for Excel 2010, 2007, 2003 and 2002 (XP). Add-in Express also provides some special features for creating an advanced user interface of your Excel add-in, such as custom task panes, XLL add-ins and more.

The sample Excel Automation addin below is written in VB.NET, while Visual C# and C++ are also supported.

Step 1. Creating an Excel Automation add-in project

Choose the Add-in Express COM Add-in project template in the New Project dialog in the Visual Studio IDE.

Excel Automation add-in project in VB.NET

Click OK to start the COM add-in project wizard. In the wizard windows, you choose the programming language, applications supported by your add-in, and interop assemblies to use. Note that since Excel Automation add-ins are supported in Excel 2002 and higher, the minimal Excel interop assembly version is XP (2002).

Excel Automation add-in solution

The wizard creates and opens a new COM Add-in solution in IDE. The solution contains an only project, the COM add-in project. Please refer to Building your first Microsoft Office COM add-in sample.

Step 2. Adding a new Excel COM Add-in module

In order to add Excel user-defined functions to the COM add-in, you add the Excel COM Add-in module to the Add-n Express COM Add-in project using the Add New Item dialog.

Adding an Excel Automation Add-in module

This adds the ExcelAddinModule1.vb (or ExcelAddinModule1.cs) file to your COM Add-in project.

Step 3. Writing an Excel user-defined function

In the Solution Explorer window, right-click the ExcelAddinModule.vb (or ExcelAddinModule.cs) file and choose the View Code item in the context menu.

Writing an Excel UDF

The module contains the following code:


Imports System.Runtime.InteropServices
'Add-in Express Excel Add-in Module
<GuidAttribute("287D044F-D233-47E6-BB48-35999635BAD3"), _
ProgIdAttribute("MyExcelAutomationAddin2.ExcelAddinModule1"), _
 ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class ExcelAddinModule1
 Inherits AddinExpress.MSO.ADXExcelAddinModule
#Region " Add-in Express automatic code "
 <ComRegisterFunctionAttribute()> _
 Public Shared Sub AddinRegister(ByVal t As Type)
 AddinExpress.MSO.ADXExcelAddinModule.ADXExcelAddinRegister(t)
 End Sub
 <ComUnregisterFunctionAttribute()> _
 Public Shared Sub AddinUnregister(ByVal t As Type)
 AddinExpress.MSO.ADXExcelAddinModule.ADXExcelAddinUnregister(t)
 End Sub
#End Region
 Public Sub New()
 MyBase.New()
 End Sub
End Class

Just add a new function to the class. Say, the following one:

 
			  
Imports Excel = Microsoft.Office.Interop.Excel
...
Public Function MyFunc(ByVal Range As Object) As Object
   MyFunc = CType(Range, Excel.Range).Value * 1000
End Function

Step 4. Running the Excel Automation add-in

Registering Excel Automation add-in

Choose Register Add-in Express Project in the Build menu (if you use the Express edition of Visual Studio, this item can be found in the context menu of the add-in module), restart Excel, and check if your add-in works.

Calling the custom user-defined function from the add-in

Step 5. Debugging your Automation add-in

To debug your add-in, specify Excel as the Start Program in the Project Options window and run the project.

Debugging the Excel Automation add-in

Step 6. Deploying the Excel Automation add-in

Create a setup project, build it, copy all setup files to the target PC and run the installer.

Deploying Excel Automation add-in

See also Deploying Add-in Express projects, Deploying Office extensions and Excel UDF tips.

Programming Office smart tags <<

>> Developing Excel XLL add-ins

Back to Add-in Express for Office and .NET homepage