Create .NET Components

If you are an experienced VBA programmer, this is a great way to start learning .NET because you can take advantage of features built in to the .NET Framework in small, incremental steps.

Note

You can use .NET components from any VBA application. There are no version restrictions and no prerequisites other than the .NET Framework.

How to do it

To create a .NET component for use in Excel:

  1. From within Visual Studio .NET, create a new Class Library project using VB.NET or C#. Visual Studio creates a folder and template files for the project as shown in Figure 5-1.

    A new, empty .NET class library project

    Figure 5-1. A new, empty .NET class library project

  2. From the Project menu, choose Add Class. Visual Studio displays the Add Project Item dialog box, as shown in Figure 5-2.

    Create a new COM class to contain components for use from Excel

    Figure 5-2. Create a new COM class to contain components for use from Excel

  3. Give the new class a descriptive name and click OK. Visual Studio registers the project to interoperate with COM (the Register for COM Interop selection on the Project Options, Build dialog box) and creates a new, empty code template for your class, as shown in Figure 5-3.

  4. Add code to the class library for the objects, properties, and methods you want to use from Excel.

  5. Compile the project by selecting Build Solution from the Build menu. Visual Studio ...

Get Excel 2003 Programming: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.