Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

↑ Back to the top


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.

↑ Back to the top


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;

↑ Back to the top


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.

↑ Back to the top


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.

↑ Back to the top


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

↑ Back to the top


Keywords: KB823987, kbevent, kbofficeauto, kbpia, kbprb

↑ Back to the top

Article Info
Article ID : 823987
Revision : 5
Created on : 2/3/2006
Published on : 2/3/2006
Exists online : False
Views : 419