我是VSTO上事件处理的新手。 我在线上进行了广泛的研究,包括挖掘Microsoft文档并尝试从支持中心应用practical but old example。
此示例的问题在于,它不适用于调用Excel.Application
的{{1}}实例。所以我尝试了自己的一个:
ThisAddin
,其中包含以下内容:。
MyEvents.vb
编译时,打开一个新工作簿并尝试双击或激活另一个工作簿:什么也没有发生。我肯定想念一些明显的东西。我觉得我缺乏通过VSTO处理事件的一些基础知识,并且很高兴提出一些关于此事的补充读物。
例如:
Imports Microsoft.Office.Interop.Excel
Module MyEvents
WithEvents ThisApp As Excel.Application '= Globals.ThisAddIn.Application
Private Sub ThisApp_SheetBeforeDoubleClick(Sh As Object, Target As Range, ByRef Cancel As Boolean) Handles ThisApp.SheetBeforeDoubleClick
Dim FromSheet As Excel.Worksheet = Sh
System.Diagnostics.Debug.Print("Event triggered: ThisApp_SheetBeforeDoubleClick in {0} or {1}", FromSheet, Target.Worksheet.Name)
End Sub
Private Sub ThisApp_WorkbookActivate(Wb As Workbook) Handles ThisApp.WorkbookActivate
System.Diagnostics.Debug.Print("Event triggered: ThisApp_WorkbookActivate")
End Sub
End Module
还是只需要WithEvents ThisApp As Excel.Application =
Globals.ThisAddIn.Application
?即我是否可以将事件绑定到给定的对象(应用程序,工作表等),或者一旦触发事件,是否需要在WithEvents ThisApp As
Excel.Application
中检查调用方,为什么?Sub
的{{1}}中吗?是否可以将其链接到Module
?Namespace
,会发生什么?它会像我目前拥有Shared WithEvents ThisApp
的实例那样运行Shared Class
吗?正如人们所看到的,我在这里有些迷茫。我想自己进行所有这些测试,但是我无法做一些应该很基础的事情。
答案 0 :(得分:0)
对于可能感兴趣的人:
Imports Microsoft.Office.Interop.Excel
Module MyEvents
WithEvents ThisApp As Excel.Application
'Start listening to Events thrown by ExcelApp
Sub StartEvents(ExcelApp as Excel.Application)
ThisApp = ExcelApp
End Sub
Private Sub ThisApp_SheetBeforeDoubleClick(Sh As Object, Target As Range, ByRef Cancel As Boolean) Handles ThisApp.SheetBeforeDoubleClick
Dim FromSheet As Excel.Worksheet = Sh
System.Diagnostics.Debug.Print("Event triggered: ThisApp_SheetBeforeDoubleClick in {0} or {1}", FromSheet, Target.Worksheet.Name)
End Sub
Private Sub ThisApp_WorkbookActivate(Wb As Workbook) Handles ThisApp.WorkbookActivate
System.Diagnostics.Debug.Print("Event triggered: ThisApp_WorkbookActivate")
End Sub
End Module
Public Class ThisAddIn
Private Sub ThisAddIn_Startup() Handles Me.Startup
MyEvents.StartEvents(Globals.ThisAddIn.Application)
End Sub
End Class