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)

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 "Symptoms" 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;

MORE INFORMATION

Steps to reproduce the problem

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

      The New Project dialog box appears.
    2. 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.
    3. Click Finish.
  2. Add the following member variables to the OfficeCodeBehind class:
    private Excel.WorkbookEvents_DeactivateEventHandler deactivateEvent;
    private Excel.WorkbookEvents_ActivateEventHandler activateEvent;
    
  3. 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;
    }
    
  4. Add handlers for the Deactivate event and the Activate event to the OfficeCodeBehind class:
    protected void ThisWorkbook_Deactivate()
    {
    	MessageBox.Show("Deactivate " + ThisWorkbook.Name);
    }
    protected void Workbook_Activate()
    {
    	MessageBox.Show("Activate " + ThisWorkbook.Name);
    }
    
  5. On the Build menu, click Build Solution.

    You may receive the build error message that is mention in the "Symptoms" 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.

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:MinorLast Reviewed:2/3/2006
Keywords:kbprb kbPIA kbOfficeAuto kbEvent KB823987 kbAudDeveloper