22

C# and Excel Integration COM Add-ins

22.1 INTRODUCTION AND OBJECTIVES

In this chapter we give an introduction to COM add-ins, what they are, how to create them and how to call them from Excel. A COM add-in is a COM component that implements a special interface called IDTExtensibility2 that is responsible for connecting to and disconnecting from Excel. The Excel client can call the add-in to compute some quantity or to transfer data between Excel and other applications, for example. It is important to note that both input to and output from the add-in are realised by Excel cells and ranges which we can access from C#.

We discuss the following topics in this chapter:

  • The IDTExtensibility2 interface and its methods OnConnection() and OnDisconnection().
  • Which code we need to write when creating a COM add-in.
  • Creating a COM add-in and using the Shared add-in wizard.
  • Using COM add-ins with Excel.
  • Initial examples.
  • An introduction to Excel-DNA.

When describing the actual steps to create a COM add-in we avoid describing which dialog boxes, check boxes and buttons to use because these tend to change between versions of Visual Studio. Instead, we describe the high-level what and how steps that show how to create add-ins. The most up-to-date Microsoft documentation should be consulted. The authors have also created some visual demos on www.datasimfinancial.com.

We assume that the reader is familiar with the most important principles underlying COM. For a clear overview of this subject, ...

Get C# for Financial Markets 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.