Capturing shape events and ActiveX events

42 views
Skip to first unread message

Stu McKenzie

unread,
Sep 5, 2025, 9:11:05 AMSep 5
to Excel-DNA
Hello,
Does anyone know if you can capture Shape events - either just shape Click, or using the shape's Hyperlink?

Alternatively, what's the best way to capture ActiveX command events? 
Is it possible to register to a global ActiveX control event, like button Click? Or do I need to iterate each worksheet and enumerate all the activeX controls
Thanks
Stu

Stu McKenzie

unread,
Sep 5, 2025, 6:40:53 PMSep 5
to Excel-DNA
I figured out the ActiveX handlers

app.WorkbookOpen += _app_WorkbookOpen;
...
foreach (MsExcel.Worksheet ws in wb.Worksheets)
    foreach (MsExcel.OLEObject obj in (MsExcel.OLEObjects)ws.OLEObjects())
        if (obj.Object is MSForms.CommandButton button)
        {
              MSForms.CommandButtonEvents_ClickEventHandler handler = () =>
              {
                  var command = button.Name;
                  // do something with command
              }
          
              handlers!.Add((button, handler));
              button.Click += handler;
          }

but I had to keep a handle on all the Button objects or they would be disposed after the first button was clicked


still - anyone know if shape events are possible though?

Stu McKenzie

unread,
Sep 6, 2025, 6:59:39 PMSep 6
to Excel-DNA
Using the ActiveX buttons seems great because I don't need VBA from the Forms buttons to trigger code in .NET. (I also user hyperlinks from cells, but they're annoying because I have to change the sheet's col/row sizing to accommodate them, good to have both options).

But one problem I've had using ActiveX buttons is that it introduces challenges to my build process: The ActiveX buttons require a COM reference to Interop.MSForms. And the dotnet build command complains that it can't compile the solution.

Oddly though, Visual Studio builds it and runs just fine. So I have to use powershell to get VS to build the code for me:

$devenv = "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\devenv.com"
cd "C:\code\mySolutionDir"
$solution = Join-Path (Get-Location) "mySolution.sln"
& $devenv $solution /Build "Release"

etc.

Does anyone know a  solution to this without requiring the COM references? Essentially I just want floating buttons (not cell links) that fire events in .NET.

Seems to work just fine this way. Sharing for reference and in case anyone else encounters this issue
Reply all
Reply to author
Forward
0 new messages