Microsoft KB Archive/823987

= Build error message when the event name and the method name are the same in Excel 2003 and in Word 2003 =

Article ID: 823987

Article Last Modified on 2/3/2006

-

APPLIES TO


 * Microsoft Visual Studio Tools for the Microsoft Office System version 2003
 * Microsoft Office Excel 2003
 * Microsoft Office Word 2003
 * Microsoft Visual C# .NET 2003 Standard Edition

-





SYMPTOMS
When you use Microsoft Visual Studio Tools for the Microsoft Office System to build a Microsoft Visual C# .NET project, you may receive a build error message that is similar to the following error message:

Microsoft.Office.Interop.Excel_Workbook.Activate denotes a 'method' which is not valid in the given context.



CAUSE
This problem occurs because of the code that is used to bind an event delegate to a method in your solution. The Microsoft Office Excel 2003 object model and the Microsoft Office Word 2003 object model may use the same name for both the method and the event that is raised when that method is executed.

You can see an example of this problem with the Activate event and with the Activate method in the Excel 2003 Workbook object. You may receive the build error message error that is mentioned in the &quot;Symptoms&quot; section if you are not explicit about whether you are addressing the Activate event or the Activate method.



RESOLUTION
To resolve this problem, use an event interface for binding an event delegate to a method in your solution. For example, to handle the Activate event for the Excel Workbook object, use code that is similar to the following code: Excel.WorkbookEvents_Event BookEvents = (Excel.WorkbookEvents_Event)thisWorkbook; activateEvent = new Excel.WorkbookEvents_ActivateEventHandler(Workbook_Activate); BookEvents.Activate += activateEvent;



Steps to reproduce the problem
 Start Microsoft Visual Studio .NET 2003. To create a new project, follow these steps:  On the File menu, point to New, and then click Project.

The New Project dialog box appears. In the Project Types list, double-click Microsoft Office System Projects, and then click Visual C# Projects. In the Templates list, click Excel Workbook, and then click OK.

The Microsoft Office Project Wizard appears. Click Finish.   Add the following member variables to the OfficeCodeBehind class: private Excel.WorkbookEvents_DeactivateEventHandler deactivateEvent; private Excel.WorkbookEvents_ActivateEventHandler activateEvent; </li>  Add the following code to the ThisWorkbook_Open event handler: protected void ThisWorkbook_Open {   deactivateEvent = new Excel.WorkbookEvents_DeactivateEventHandler(ThisWorkbook_Deactivate); thisWorkbook.Deactivate += deactivateEvent;

activateEvent = new Excel.WorkbookEvents_ActivateEventHandler(Workbook_Activate); thisWorkbook.Activate += activateEvent; } </li>  Add handlers for the Deactivate event and the Activate event to the OfficeCodeBehind class:

protected void ThisWorkbook_Deactivate {   MessageBox.Show(&quot;Deactivate &quot; + ThisWorkbook.Name); } protected void Workbook_Activate {   MessageBox.Show(&quot;Activate &quot; + ThisWorkbook.Name); } </li> On the Build menu, click Build Solution.

You may receive the build error message that is mention in the &quot;Symptoms&quot; section.</li></ol>

<div class="workaround_section">

WORKAROUND
To work around the build error, replace the code in the ThisWorkbook_Open event handler with the following code:

protected void ThisWorkbook_Open {   deactivateEvent = new Excel.WorkbookEvents_DeactivateEventHandler(        ThisWorkbook_Deactivate); thisWorkbook.Deactivate += deactivateEvent;

Excel.WorkbookEvents_Event BookEvents = (Excel.WorkbookEvents_Event)thisWorkbook; activateEvent = new Excel.WorkbookEvents_ActivateEventHandler(Workbook_Activate); BookEvents.Activate += activateEvent; }

The project now builds without an error. The events are handled as expected.

<div class="references_section">