Search code examples
delphiexcelvbadllidispatch

Writing a DLL for Excel in Delphi


I'm using Turbo Delphi 2006.

The DLL will be called from within Excel as part of a VBA/DLL combination.

The first part of the problem is trying to find out how to pass to the DLL a reference to the current Excel session. Most other code I've seen was that it launched a separate instance of Excel apart from the one you're in.

I've seen some C++ code that creates an instance of IDispatch and then passes something in to a method of the IDispatch object, but not knowing much C++.

Any ideas?


Solution

  • What you describe is called writing a COM addin. You need to create an automation DLL and implement the IDTExtensibility2 interface. You will then receive the Excel Application interface as a parameter to the OnConnection method.

    You will also need to register your DLL as an addin so Excel will automatically load it.

    EDIT: Forgot to mention: You might want to take a look at Add-in Express. Their framework and components make getting started with the creation of Office addins ridiculuously easy. You definitely won't have to bother with the details of IDTExtensibility2. All that comes with a (well-justified) price tag, though.