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):

Method

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

Event

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s


%d bloggers like this: