Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I resolve the ambiguity in this Microsoft.Office.Excel method call?

Tags:

c#

excel

I am using office 2007 excel work sheet function in c# code. VS2010 issues this warning

Warning 3 Ambiguity between method 'Microsoft.Office.Interop.Excel._Worksheet.Activate()' and non-method 'Microsoft.Office.Interop.Excel.DocEvents_Event.Activate'. Using method group. D:\EXLANEDB01p\dev\libraries\EXCEL\Excel.cs 531 95 EXCEL

How do I resolve this ? the call is

xSheet.Activate(); 

where xSheet passed as ref in the call as

ref Microsoft.Office.Interop.Excel.Worksheet xSheet
like image 830
TonyP Avatar asked Jul 24 '10 11:07

TonyP


1 Answers

You need to disambiguate the Activate name. Within the Worksheet interface, Activate is either a method (if looked at as a _Worksheet object) or an event (if looked at as a DocEvents_Event object).

Cast your object to Microsoft.Office.Interop.Excel._Worksheet first then call Activate().

((Microsoft.Office.Interop.Excel._Worksheet)xSheet).Activate();

Or declare a new _Worksheet variable and use that within your method.

_Worksheet sheet = xSheet;
sheet.Activate();

Otherwise you could change your method declaration to take a reference to a _Worksheet instead of a Worksheet as well as all associated declarations.

void MyMethod(ref Microsoft.Office.Interop.Excel._Worksheet xSheet)
{
    // ...
}

// usage:
_Worksheet sheet = new Worksheet();
MyMethod(ref sheet);
like image 183
Jeff Mercado Avatar answered Oct 23 '22 16:10

Jeff Mercado