C# Excel SheetBeforeDoubleClick capture from COM Addin

Mar 21, 2014 at 10:03 AM
I've created a C# COM Excel Add-In, but am struggling to capture and recognise a double click event from Excel in C#?

e.g. in VBA I would add the below into the 'ThisWorkbook' module:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Cancel = True
MsgBox Target.Address
End Sub
I could use this to trigger stuff in the C# add-in, but thought it would be more fluent to have the add-in respond to the double click event in Excel, rather then trying to add the above programmatically into Excel workbooks.

I'm trying to add Event Handlers, like below, but am struggling truth be told.

Excel.Application app = application as Excel.Application;
app.SheetBeforeDoubleClick += Excel.AppEvents_SheetBeforeDoubleClickEventHandler(beforeDoubleClick);
Any advice please?
Coordinator
Mar 21, 2014 at 11:02 AM
Edited Mar 21, 2014 at 11:07 AM
Start with a new C# Class Library, then install the Excel-DNA package from NuGet.
You need a reference Microsoft.Office.Interop.Excel. (Installing the Excel-DNA.Interop package will also add this).

Your code to install the event handler might look like this:
using Excel = Microsoft.Office.Interop.Excel;
using ExcelDna.Integration;

namespace GeneralTestsCS
{
    public class TestAddIn : IExcelAddIn
    {
        public Excel.Application Application;
        public void AutoOpen()
        {
            Application = (Excel.Application)ExcelDnaUtil.Application;
            Application.SheetBeforeDoubleClick += Application_SheetBeforeDoubleClick; 
        }

        void Application_SheetBeforeDoubleClick(object Sh, Excel.Range Target, ref bool Cancel)
        {
            Application.StatusBar = "DoubleClicked on " + Target.Address;
        }

        public void AutoClose()
        {
        }
    }
}

(+ is actually "plus". I think CodePlex is feeling ill today.
Mar 21, 2014 at 11:29 AM
Thank you! I'm not sure what I was missing, but that works perfectly.