Hi,
You need no special development environment to enable any of the Excel-
DNA features. One you have Excel-DNA and the .NET runtime installed,
it can all be done in our favourite text editor, or you can use a free
IDE like one of the Visual Studio Express editions or SharpDevelop
(
http://www.sharpdevelop.net/OpenSource/SD/Default.aspx). Ross McLean
wrote a very nice series of blogposts on getting started with Excel-
DNA, including how to use SharpDevelop:
http://www.blog.methodsinexcel.co.uk/2010/09/22/writing-an-excel-dna-function-using-an-ide/.
Dealing with the Excel events is possible with Excel-DNA. There are
two approaches:
1. Using the C API (XlCall class in Excel-DNA). You only have a small
number of events, but they should work on all versions of Excel with
no other dependencies. To handle these you create a macro in Excel-
DNA, and then call XlCall.OnXXXX, "MyMacro" to set up the handler.
The events available through the C API are:
xlcOnDoubleClick
xlcOnEntry - user entered data into a cell
xlcOnKey - user pressed a keystroke
xlcOnRecalc
xlcOnSheet
xlcOnWindow - worksheet switch
xlcOnTime - scheduled macro
2. Using the COM API. This way you can access all the events you know
from VBA, but need to learn about "Primary Interop Assemblies", and
will have to take some care dealing with different versions of Excel.
I think some other Excel-DNA users would know more about how to
redistribute the required assemblies and deal with different Excel
versions.
(In theory it is also possible to deal with the COM events in a late-
bound way, without being tied to a specific Excel version or having to
redistribute the version-specific PIA assemblies. But there is no
built-in support for this in Excel-DNA at present, and it would take
quite a bit of work to make something that works safely for the
various Excel versions.)
The example below exhibits both ways of event handling, and works on
my machine with Excel 2007.
For the example to work on your side, you probably need to install the
PIA assemblies, which can be found here:
Excel 2003 -
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=3c9a983a-ac14-4125-8ba0-d36d67e0f4ad&DisplayLang=en
Excel 2007 -
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=59DAEBAA-BED4-4282-A28C-B864D8BFA513
To run the example, create a text file (called EventTest.dna) with
everything from the <DnaLibrary> and </DnaLibrary> tags. Then copy the
file ExcelDna.xll from the Excel-DNA Distribution directory and call
it EventTest.xll, and File->Open it in Excel.
(Of course this could all be in a compiled library too.)
If you have more specific questions, I'm happy to make an example to
help you get started.
Regards,
Govert
<DnaLibrary Name="Excel-DNA Event Test">
<Reference Name="System.Windows.Forms" />
<Reference Name="Microsoft.Office.Interop.Excel" />
<![CDATA[
Imports SWF = System.Windows.Forms
Imports Microsoft.Office.Interop.Excel
Imports ExcelDna.Integration
Public Class MyAddIn
Implements IExcelAddIn
Dim WithEvents App As Application
Public Sub AutoOpen() Implements IExcelAddIn.AutoOpen
SWF.MessageBox.Show("Opening the Excel-DNA Event Test add-in")
' Set the local App variable,
' which implicly hooks up the SheetActive handler
App = ExcelDnaUtil.Application
' Explicitly add a handler for the
' Application's SheetBeforeDoubleClick event
AddHandler App.SheetBeforeDoubleClick, AddressOf
MyDoubleClickHandler
End Sub
Public Sub AutoClose() Implements IExcelAddIn.AutoClose
End Sub
Private Sub Application_SheetActivate(ByVal sh As Object) _
Handles App.SheetActivate
System.Windows.Forms.MessageBox.Show("SheetActivate Handled: "
+ sh.Name)
End Sub
Private Sub MyDoubleClickHandler(ByVal sheet As Object, _
ByVal target As Range, _
ByRef cancel As Boolean)
Dim message As String = String.Format( _
"Sheet {0} double-clicked at {1}.", sheet.Name,
target.Address)
SWF.MessageBox.Show(message)
End Sub
End Class
Public Module TestEvents
<ExcelCommand(MenuName:="Event Test", MenuText:="Install Calculate
Handler")> _
Sub InstallCalculateHandler()
XlCall.Excel(XlCall.xlcOnRecalc, ExcelMissing.Value,
"MyOnRecalcHandler")
End Sub
<ExcelCommand(MenuName:="Event Test", MenuText:="Remove Calculate
Handler")> _
Sub UninstallCalculateHandler()
XlCall.Excel(XlCall.xlcOnRecalc)
End Sub
Sub MyOnRecalcHandler()
XlCall.Excel(XlCall.xlcAlert, "OnRecalc event received ...")
End Sub
End Module
]]>
</DnaLibrary>