HOW TO: Handle Events for the Office 2003 Web Components in Visual Studio .NET

Article translations Article translations
Article ID: 823982 - View products that this article applies to.
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article describes how to modify the class wrappers for Microsoft Office 2003 Web Components. It then describes how to regenerate the class wrappers so that you can manage their events in an application that you create by using Microsoft Visual Studio .NET.

By default, the class wrappers that Microsoft Visual Studio .NET generates do not permit you to handle events for the following Office 2003 Web components:
  • Chart
  • Spreadsheet
  • PivotTable
To handle the events for these components, you can modify the source code for the class wrappers, and then recompile the code.

Background

Microsoft Visual Studio .NET generates an event listener class that implements an event interface. This event listener class is passed to the Office Web Components (OWC). The OWC then calls the QueryInterface method for the IDispatch interface, instead of calling the event interface. However, the IDispatch interface for the generated class does not contain the dispatch identifiers (DISPIDs) for the events.

To resolve this issue, you can mark the generated class by using the ClassInterfaceType.None value to force COM interop to return IDispatch for the interface instead of for the class. The IDispatch interface contains the event DISPIDs.

Steps to Modify and to Rebuild the Class Wrappers

  1. Start the Visual Studio .NET command prompt.
  2. At the command prompt, do the following:
    1. Create a new directory named owc11, and then change to that directory:
      md c:\owc11

      cd c:\owc11
    2. Generate the source code in C# for the Microsoft Windows Forms wrapper:
      aximp "c:\program files\common files\microsoft shared\web components\11\owc11.dll" /source
    3. Delete the generated assembly AxOWC11.dll:
      del c:\owc11\axowc11.dll
  3. In a text editor (such as Notepad), open C:\OWC11\AxOWC11.cs.
  4. Add [ToolboxItem(true)] to the generated class for the Chart component, the Spreadsheet component, and the PivotTable component, as in the following examples:

    Chart Component
     [System.ComponentModel.ToolboxItem(true)] 
     public class AxChartSpace : System.Windows.Forms.AxHost { 
      	... 
     } 
    Spreadsheet Component
     [System.ComponentModel.ToolboxItem(true)] 
     public class AxSpreadsheet : System.Windows.Forms.AxHost {
     	 ... 
     } 
    PivotTable Component
     [System.ComponentModel.ToolboxItem(true)] 
     public class AxPivotTable : System.Windows.Forms.AxHost {
     	 ... 
     } 
  5. Mark the event classes as ClassInterfaceType.None, as in the following examples:

    Chart Component
     [System.Runtime.InteropServices.ClassInterface( 
      System.Runtime.InteropServices.ClassInterfaceType.None)] 
     public class AxChartSpaceEventMulticaster : OWC11.IChartEvents {
     	 ...
      } 
    Spreadsheet Component
     [System.Runtime.InteropServices.ClassInterface( 
      System.Runtime.InteropServices.ClassInterfaceType.None)]
     public class AxSpreadsheetEventMulticaster : OWC11.ISpreadsheetEventSink {
     	 ...
     } 
    PivotTable Component
     [System.Runtime.InteropServices.ClassInterface( 
      System.Runtime.InteropServices.ClassInterfaceType.None)] 
     public class AxPivotTableEventMulticaster : OWC11.IPivotControlEvents {
     	 ... 
     } 
  6. Save your changes to AxOWC11.cs.
  7. At the Visual Studio .NET command prompt, do the following:
    1. Add C:\Program Files\Microsoft.NET\Primary Interop Assemblies to your LIB environment variable:
      set lib=c:\program files\microsoft.net\primary interop assemblies;%path%
    2. Copy Microsoft.Office.Interop.OWC11.dll and MSComCtl.dll from the GAC where the Microsoft Office 2003 Primary Interop Assemblies are installed:
      copy %windir%\assembly\GAC\Microsoft.Office.Interop.Owc11\11.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Owc11.dll

      copy %windir%\assembly\GAC\mscomctl\10.0.4504.0__31bf3856ad364e35\mscomctl.dll
    3. Compile the source file, AxOWC11.cs:
      csc /t:library /r:microsoft.office.interop.owc11.dll /r:adodb.dll /r:msdatasrc.dll /r:mscomctl.dll axowc11.cs
      Note You receive the following CS0108 error message:
      The keyword new is required
      You may ignore this error.
  8. Start Visual Studio .NET.
  9. Add your newly-compiled AxOWC11.dll to the Toolbox:
    1. On the Tools menu, click Customize Toolbox (in Visual Studio .NET 2002), or click Add/Remove Toolbox Items (in Visual Studio .NET 2003), and then click the .NET Framework Components tab.
    2. On the .NET Framework Components tab, click Browse.
    3. In the File name text box, type c:\owc11\axowc11.dll. Click Open.
    4. Click OK to close the Customize Toolbox dialog box.
When you create a new Microsoft Visual Basic .NET project or a new Microsoft Visual C# .NET project, notice that the list of controls on the General tab in the Toolbox contains the AxChartSpace control, the AxPivotTable control, and the AxSpreadsheet control. You can now add these components to your forms.

REFERENCES

For additional information about managing events for the Office XP Web Components by using Visual Studio .NET, click the following article numbers to view the articles in the Microsoft Knowledge Base:
328275 HOW TO: Handle Events for the Office Web Components in Visual Studio .NET

Properties

Article ID: 823982 - Last Review: September 17, 2003 - Revision: 1.5
APPLIES TO
  • Microsoft Office 2003 Web Components
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbpia kbofficewebspread kbofficewebpivot kbofficewebchart kbhowto KB823982

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com