Ambiguity using Worksheet Activate

Activate is both a method and an event of Application.Excel, so the Activate keyword must be cast to either an event or method.

Examples (based on a VSTO Excel 2007 Add-in):


Excel.Workbook workbook = Globals.ThisAddIn.Application.ActiveWorkbook;
Excel.Worksheet worksheet = workbook.Sheets["Sheet2"] as Excel.Worksheet;
//The next line will throw a warning
//Need to cast as a method
((Microsoft.Office.Interop.Excel._Worksheet)worksheet).Activate() ;


((Excel.DocEvents_Event)worksheet).Activate += 
                new Microsoft.Office.Interop.Excel.DocEvents_ActivateEventHandler(

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: