Microsoft Visual Studio .NET 生成一个事件侦听程序类,该类实现一个事件接口。该事件侦听程序类将被传递到 Office Web 组件 (OWC)。随后,OWC 调用 IDispatch 接口的 QueryInterface 方法,而不是调用事件接口。但是,生成的类的 IDispatch 接口不包含事件的调度标识符 (DISPID)。
若要解决此问题,您可以使用 ClassInterfaceType.None 值标记生成的类以强制 COM Interop 为接口(而不是类)返回 IDispatch。IDispatch 接口包含事件 DISPID。
修改和重新生成类包装的步骤
- 启动 Visual Studio .NET 命令提示符。
- 在命令提示符下,执行以下操作:
- 创建一个名为 owc11 的新目录,然后更改到该目录:
md c:/owc11
cd c:/owc11 - 生成 Microsoft Windows 窗体包装的源代码(C# 语言):
aximp "c:/program files/common files/microsoft shared/web components/11/owc11.dll" /source
- 删除生成的程序集 AxOWC11.dll:
del c:/owc11/axowc11.dll
- 创建一个名为 owc11 的新目录,然后更改到该目录:
- 在文本编辑器(例如“记事本”)中,打开“C:/OWC11/AxOWC11.cs”。
- 将 [ToolboxItem(true)] 添加到图表组件、电子表格组件和数据透视表组件的生成的类中,如下面的示例所示:
图表组件[System.ComponentModel.ToolboxItem(true)] public class AxChartSpace : System.Windows.Forms.AxHost { ... }
电子表格组件[System.ComponentModel.ToolboxItem(true)] public class AxSpreadsheet : System.Windows.Forms.AxHost { ... }
数据透视表组件[System.ComponentModel.ToolboxItem(true)] public class AxPivotTable : System.Windows.Forms.AxHost { ... }
- 将事件类标记为 ClassInterfaceType.None,如下面的示例所示:
图表组件[System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.None)] public class AxChartSpaceEventMulticaster : OWC11.IChartEvents { ... }
电子表格组件[System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.None)] public class AxSpreadsheetEventMulticaster : OWC11.ISpreadsheetEventSink { ... }
数据透视表组件[System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.None)] public class AxPivotTableEventMulticaster : OWC11.IPivotControlEvents { ... }
- 将您的更改保存到 AxOWC11.cs。
- 在 Visual Studio .NET 命令提示符下,执行以下操作:
- 将 C:/Program Files/Microsoft.NET/Primary Interop Assemblies 添加到您的 LIB 环境变量中:
set lib=c:/program files/microsoft.net/primary interop assemblies;%path%
- 从安装 Microsoft Office 2003 主互操作程序集的 GAC 中复制 Microsoft.Office.Interop.OWC11.dll 和 MSComCtl.dll:
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 - 编译源文件 AxOWC11.cs:
csc /t:library /r:microsoft.office.interop.owc11.dll /r:adodb.dll /r:msdatasrc.dll /r:mscomctl.dll axowc11.cs
注意 您会收到以下 CS0108 错误信息:
The keyword new is required您可以忽略此错误。
- 将 C:/Program Files/Microsoft.NET/Primary Interop Assemblies 添加到您的 LIB 环境变量中:
- 启动 Visual Studio .NET。
- 将新编译的“AxOWC11.dll”添加到“工具箱”中:
- 在“工具”菜单上,单击“自定义工具箱”(在 Visual Studio .NET 2002 中)或单击“添加/删除工具箱项”(在 Visual Studio .NET 2003 中),然后单击“.NET Framework 组件”选项卡。
- 在“.NET Framework 组件”选项卡上,单击“浏览”。
- 在“文件名”文本框中,键入 c:/owc11/axowc11.dll。单击“打开”。
- 单击“确定”以关闭“自定义工具箱”对话框。
当创建新的 Microsoft Visual Basic .NET 项目或新的 Microsoft Visual C# .NET 项目时,您会注意到“工具箱”中“常规”选项卡上的控件列表中包含“AxChartSpace”控件、“AxPivotTable”控件和“AxSpreadsheet”控件。现在便可以将这些组件添加到窗体中了。
参考
有关使用 Visual Studio .NET 管理 Office XP Web 组件事件的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:328275 HOW TO:Handle Events for the Office Web Components in Visual Studio .NET