difficulty adding Worksheet.Activate event

Category: visual studio officetools


kavm on Wed, 03 Jun 2009 17:20:21

I want to add worksheet.activate (and worksheet.deactivate event to each of my worksheets in a c# VSTO Excel customization project in VS2008.

For the Deactivate event, it was simple. I put statement in each (partial) sheet class: this.Deactivate += ... to add the event handler. Activate event is proving a lot harder. Following Carter / Lipper book discussion - I have tried to add statement in my Sheet1_Startup() :

(Excel.DocEvents_Event)this.Activate += new Microsoft.Office.Interop.Excel.DocEvents_ActivateEventHandler(Sheet1_Activate);

However, I get a compile error saying:
Cannot convert type "MyNameSpace.Sheet1" to Microsoft.Office.Interop.Excel.DocEvents_Event" - and I am stuck. Replacing this by Globals.Sheet1 does not have an effect.

Any advice would be much appreciated.

Just wanted to add that similar handler on Deactivate compiles fine:
this.Deactivate += new Microsoft.Office.Interop.Excel.DocEvents_DeactivateEventHandler(Sheet1_Deactivate);
So, the Sheet1 object does take these handlers.


kavm on Wed, 03 Jun 2009 23:25:40

OK. So - I did spot that I can use the ActivateEvent handler as follows:

void Sheet1_Startup(...)
    this.ActivateEvent += new Microsoft.Office.Interop.Excel.DocEvents_ActivateEventHandler(Sheet1_ActivateEvent);

I gather this is on the aggregated object. I was thrown off by the discussion of casting worksheet with (Excel.DocEvents_Event), etc.

JiessieHowe on Thu, 28 Mar 2019 02:12:28

How can I do the worksheet activation event?

I am want to use C # to create an Excel 2013 and 2016 VSTO template

Execute programs when switching worksheets, such as incrementing Cells [1,1] by 1 in the activated worksheet cell

ask for advice