Build error message when the event name and the method name are the same in Excel 2003 and in Word 2003 (823987)
The information in this article 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)
SYMPTOMSWhen 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. CAUSEThis 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 "Symptoms" section if you are not explicit about whether you are addressing the Activate event or the Activate method.RESOLUTIONTo 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; WORKAROUNDTo 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. REFERENCES
For additional information about how to automate Excel 2003 from Visual C# .NET, click the following article numbers to view the articles in the Microsoft Knowledge Base:
302084
HOWTO: Automate Microsoft Excel from Microsoft Visual C# .NET
302815 HOW TO: Handle events for Excel by using Visual C# .NET
311452 INFO: Develop Microsoft Office solutions with Visual Studio .NET
Modification Type: | Minor | Last Reviewed: | 2/3/2006 |
---|
Keywords: | kbprb kbPIA kbOfficeAuto kbEvent KB823987 kbAudDeveloper |
---|
|