You are currently offline, waiting for your internet to reconnect

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

This article was previously published under Q328275
SUMMARY
The default class wrappers that are generated by Microsoft Visual Studio .NET do not allow you to handle events for the Office XP Chart, Spreadsheet, and PivotTable Web components. To handle the events for these components, you can modify the source code for these class wrappers, and then recompile. This article describes how to modify and how to regenerate the class wrappers for the Office XP Web Components so that you can manage their events in a Visual Studio .NET application.

back to the top

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 for the IDispatch interface, instead of 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 with the ClassInterfaceType.None value to force COM Interop to return IDispatch for the interface instead of for the class. The IDispatch interface does contain the event DISPIDs.

back to the top

Steps to Modify and to Rebuild Class Wrappers

NOTE: The steps in this article assume that you have installed the Microsoft Office XP Primary Interop Assemblies (PIAs). For additional information about the Office XP PIAs, click the article number below to view the article in the Microsoft Knowledge Base:
328912 INFO: Office XP PIAs Available for Download
  1. Start the Visual Studio .NET command prompt.
  2. Create a new directory, and then change to that directory:
       md c:\owc10   cd "c:\owc10"						
  3. Generate the source code for C# for the Windows forms wrapper:
       aximp "c:\program files\common files\microsoft shared\web components\10\owc10.dll" /source						
  4. Delete the generated assembly AxOWC10.dll:
       del c:\owc10\axowc10.dll						
  5. Open C:\OWC10\AxOWC10.cs in any text editor.
  6. Add [ToolboxItem(true)] to the generated class for the Chart, the Spreadsheet, and the PivotTable components. For example:
        [System.ComponentModel.ToolboxItem(true)]    public class AxChartSpace : System.Windows.Forms.AxHost {        ...    }					
    -and-
        [System.ComponentModel.ToolboxItem(true)]    public class AxSpreadsheet : System.Windows.Forms.AxHost {        ...    }					
    -and-
        [System.ComponentModel.ToolboxItem(true)]    public class AxPivotTable : System.Windows.Forms.AxHost {        ...    }					
  7. Mark the event classes as ClassInterfaceType.None:
        [System.Runtime.InteropServices.ClassInterface(     System.Runtime.InteropServices.ClassInterfaceType.None)]    public class AxChartSpaceEventMulticaster : OWC10.IChartEvents {        ...    }					
    -and-
        [System.Runtime.InteropServices.ClassInterface(     System.Runtime.InteropServices.ClassInterfaceType.None)]    public class AxSpreadsheetEventMulticaster : OWC10.ISpreadsheetEventSink {        ...    }					
    -and-
        [System.Runtime.InteropServices.ClassInterface(     System.Runtime.InteropServices.ClassInterfaceType.None)]        public class AxPivotTableEventMulticaster : OWC10.IPivotControlEvents {        ...    }					
  8. Save your changes to AxOWC10.cs, and then return to the command prompt.
  9. 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%						
  10. Copy Microsoft.Office.Interop.OWC.dll and MSComCtl.dll from the directory where you have installed the Microsoft Office XP Primary Interop Assemblies:
       copy c:\officexppias\microsoft.office.interop.owc.dll   copy c:\officexppias\mscomctl.dll						
  11. Compile the source file, AxOWC10.cs:
       csc /t:library /r:microsoft.office.interop.owc.dll /r:adodb.dll /r:msdatasrc.dll /r:mscomctl.dll axowc10.cs							
    NOTE: You receive warning CS0108, "The keyword new is required".

    You may ignore this error.
  12. Start Visual Studio .NET.
  13. Add your newly-compiled AxOWC10.dll to the toolbox:
    1. On the Tools menu, click Customize Toolbox.
    2. On the .NET Framework Components tab, click Browse.
    3. Type c:\owc10\axowc10.dll, and then click Open.
    4. Click OK to dismiss the Customize Toolbox dialog box.
back to the top

REFERENCES
For additional information about managing events for the Office Web Components by using Visual Studio .NET, click the article numbers below to view the articles in the Microsoft Knowledge Base:
319342 HOW TO: Handle Events for the Office XP Spreadsheet Component on a Windows Form in Visual Basic .NET
319341 HOW TO: Handle Events for the Office XP Spreadsheet Component on a Windows Form in Visual C# .NET
319557 HOW TO: Handle Events for the Office XP Chart Component by Using Visual Basic .NET
319559 HOW TO: Handle Events for the Office XP Chart Component on a Windows Form in Visual C# .NET
back to the top
webchart pivot table spread sheet excel owc
Properties

Article ID: 328275 - Last Review: 07/30/2003 14:02:50 - Revision: 3.1

  • Microsoft Office XP Web Components
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • kbhowtomaster kbofficewebchart kbofficewebpivot kbofficewebspread KB328275
Feedback