1

I am tracking user changes to a sheet:

((Excel.DocEvents_Event)app.ActiveSheet).Change +=
    new Excel.DocEvents_ChangeEventHandler(Event_WorksheetChange);

If I change a cell's value, then my function triggers:

private void Event_WorksheetChange(Excel.Range target)
{
    // do something here
}

This works perfectly until I send/receive data over a TCP connection. Specifically, the event handler triggers every time if I do something that isn't TCP based. But once I send and receive messages, then eventually the event handler just stops triggering!

It's not an immediate thing in that I can send/receive a few messages while the handler still triggers. But after a dozen messages or so, the handler stops.

Also, I can "reactivate" the handler by calling the above logic to update Change again. It's as if my handler had simply been removed and that I can re-add it.

I'm using Excel 365 and Visual Studio 2022.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
  • It may be a thread issue. Excel events are COM Connection points https://devblogs.microsoft.com/oldnewthing/20130611-00/?p=4113 with UI such as Excel, you probably need to pump Windows message (https://stackoverflow.com/questions/3152011/why-are-translatemessage-and-dispatchmessage-separate-calls) all time, which may be blocked by your TCP thing. Difficult to say w/o more detail and reproducible code. – Simon Mourier May 11 '23 at 07:09
  • First, are you running this on a PC, Mac, Server? Second *this is totally left field suggestion* but worth sharing as when you know about it, it could explain why **triggers every time if I do something that isn't TCP based** and `Excel messages` are `being filtered` see Hans answer: https://stackoverflow.com/a/10564216/495455 then see how this guy fixed it: https://stackoverflow.com/q/30948205/495455 – Jeremy Thompson May 12 '23 at 07:04
  • @JeremyThompson This is on Windows 10 Pro. – chrisaycock May 12 '23 at 15:32

0 Answers0