如何在Microsoft脚本控制中实现事件?

我在MSScriptControl上读过的所有文档都说它可以响应添加到它的对象的事件。

脚本控件允许您编写在发生对象事件时自动触发的脚本。 https://msdn.microsoft.com/en-us/library/ms974586.aspx

ScriptControl将能够吸收使用AddObject方法添加的对象生成的事件。 http://flylib.com/books/en/1.492.1.154/1/

但是我没有成功这么做。 我认为这意味着当添加的对象引发事件时,ScriptControl中的代码将会触发。 我无法得到任何工作。

寻求将任何对象添加到ScriptControl的示例代码,以及处理该控件触发的事件。 不要在意对象是自定义类,窗体,控件还是内置Excel对象(如Worksheet)。

在Win Server 2008 64bit上运行Office 2010 32位。

打开到替代方法,如WSH,但Tushar Mehta没有在这里成功http://dailydoseofexposit.com/archives/2009/08/19/using-vbscript-to-monitor-office-eventsor-not/

我已经成功地将Excel应用程序对象添加到ScriptControl,并在Excel应用程序对象上执行代码:

这工作没问题:

Function TestProc() Dim oScriptCtl As New MSScriptControl.ScriptControl With oScriptCtl ' init .Language = "VBScript" .AllowUI = True ' add Excel application object .AddObject "app", Application, True ' add procedure Dim sCode As String sCode = "Sub TestProc : MsgBox ""hi"" : End Sub" .AddCode sCode ' run procedure. Msgbox displays. .Run "TestProc" End With ' cleanup Set oScriptCtl = Nothing End Function 

失败:

在这个testing中,m_oScriptCtl是一个模块范围的variables。 当我点击表单时没有任何反应:

 Function TestForm() Set m_oScriptCtl = New MSScriptControl.ScriptControl With m_oScriptCtl ' init .Language = "VBScript" .AllowUI = True MyForm.Show False .AddObject "app", Application, True .AddObject "frm", MyForm, True .State = Connected Dim sCode As String sCode = "Sub frm_Click(): MsgBox Chr(14): End Sub" .AddCode sCode End With End Function 

下一个在.AddCode上报告以下错误:

预期')'

 Function TestSheet() Set m_oScriptCtl = New MSScriptControl.ScriptControl With m_oScriptCtl ' init .Language = "VBScript" .AllowUI = True .AddObject "app", Application, True .AddObject "sheet", Sheet2, True .State = Connected Dim sCode As String sCode = "Private Sub sheet_Change(ByVal Target As Range): MsgBox Target: End Sub" .AddCode sCode End With End Function 

在下一个testing中,MyClass被定义为:

 Public Event MyEvent() Public Sub TestEvent() RaiseEvent MyEvent End Sub 

但以下报告“对象不支持属性或方法”上.Run。 所以在这种情况下,这不是失败的事件 – 我只是不能在类中运行一个方法。

 Function TestClassEvent() Set oScriptCtl = New MSScriptControl.ScriptControl Dim oClass As New MyClass With oScriptCtl ' init .Language = "VBScript" .AllowUI = True ' add objects .AddObject "app", Application, True .AddObject "oClass", oClass, True .State = Connected ' add code Dim sCode As String sCode = "Sub oClass_MyEvent() : MsgBox vbNullString : End Sub" .AddCode sCode .Run "oClass.TestEvent" End With ' cleanup Set oScriptCtl = Nothing End Function 

线索:

有人张贴:

如果你完全没有沉没你的事件,尝试调用'ScriptControl1.Modules(“Global”)。CodeObject.Name_Of_Your_Event(ParameterList)' http://computer-programming-forum.com/59-vbscript/4b059f9f6eacfaf0.htm

– 但是这个解决方法对我来说还不清楚:事件过程不应该被明确地“调用”,它们应该只是被触发。 在上面的TestClassEvent示例中,以下几行都提供了“找不到方法或数据成员”:

 m_oScriptCtl.Modules("Global").CodeObject.MyEvent m_oScriptCtl.Modules("Global").CodeObject.TestEvent 

我没有testing以下,因为我不太确定如何:

脚本控件无法处理与https://diigo.com/08we68中托pipe的应用程序在同一项目中的类中的事件

不知道以下是否相关,不太明白: http : //www.programmersheaven.com/discussion/79452/me-activecontrol-and-events

使其工作的关键是 :您必须将两个事件都添加到脚本控制之后 ,才能在侦听器类中设置事件触发对象。 意思是,这条线必须在SC内执行:

Set oListener.EventFiringObject = oEventFiringObject

这是一个触发和响应脚本控件内的对象之间事件的工作示例。

在这个例子中:

  • 我演示了2种事件触发对象:自定义类和工作表。
  • 在将其添加到scriptcontrol(“sc”)之前,将自定义类实例化。
  • 我在自定义sc对象中调用一个方法。

设置演示

  • 开始一个新的项目(即在Excel中添加一个新的工作簿)。
  • 在您的VB IDE中,添加对Microsoft脚本控制的引用
  • 创build下面的VB组件:

clsSheetListener

 Public WithEvents oSht As Worksheet Private Sub oSht_Change(ByVal Target As Range) ' show changed cell MsgBox "Sheet Listener" & vbCrLf & "Changed: " & Target.Address _ & vbCrLf & Target.Cells(1).Value2 End Sub 

clsEventClass

 Public Event MyEvent(sCaller As String) Public Sub Raise_MyEvent(sCaller As String) RaiseEvent MyEvent(sCaller) End Sub 

clsClassListener

 Public WithEvents m_oEventClass As clsEventClass Private Sub m_oEventClass_MyEvent(sCaller As String) ' show my execution-scope MsgBox "Class Listener, " & sCaller & " caller" End Sub 

模块模块Module1

 Function Main() ' init scriptcontrol Set m_oScriptCtl = Nothing Set m_oScriptCtl = New MSScriptControl.ScriptControl With m_oScriptCtl .Language = "VBScript" .AllowUI = True ' add Excel application object, needed for all Excel methods in script-control .AddObject "sc_Application", Application, True ' add Sheet2 to the sc ' code executed in sc refers to objects by name, as defined in .AddObject .AddObject "sc_oSheet", Sheet2, True ' init sheet event-listener, and add to sc Dim oSheetListener As New clsSheetistener .AddObject "sc_oSheetListener", oSheetListener, True ' register the sheet-object with its listener in the scriptcontrol ' so the listener can hear the sheet's events .ExecuteStatement "Set sc_oSheetListener.oSht = sc_oSheet" ' init custom event-firing class object, and add to sc Dim oEventClass As New clsEventClass .AddObject "sc_oEventClass", oEventClass, True ' init class-event listener, and add to sc Dim oClassListener As New clsClassListener .AddObject "sc_oClassListener", oClassListener, True ' register event-firing object with its listener inside the Script Control ' so the listener can hear the object's events .ExecuteStatement "Set sc_oClassListener.m_oEventClass = sc_oEventClass" ' cause event to be raised. ' Call from local context, then sc-context. ' it's the same object instance in both cases oEventClass.Raise_MyEvent "Local" .ExecuteStatement "sc_oEventClass.Raise_MyEvent ""SC""" End With End Function 

testing

通过Main() 。 你会看到Raise_MyEventclsEventClass触发MyEvent时, clsClassListener用消息框响应事件。

现在切换到Excel前端,并在Sheet2的单元格中input一个值。 你会看到clsSheetListener用一个消息框来响应Change事件。