2016-09-17 38 views
3

我在MSScriptControl上讀過的所有文檔都說它可以響應添加到它的對象的事件。如何在Microsoft腳本控制中實現事件?

腳本控制允許您編寫腳本,該腳本會在發生對象上的事件時自動觸發 。 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沒有與此http://dailydoseofexcel.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 

失敗:

在此測試中,m_oScriptCtl是一個模塊範圍的變量。沒有任何反應,當我點擊形式:

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 

在接下來的測試中, 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( 「環球」)CodeObject。Name_Of_Your_Event(參數列表)」 http://computer-programming-forum.com/59-vbscript/4b059f9f6eacfaf0.htm

- 但解決辦法是我不清楚:不應該事件過程被‘叫’明確,他們應該只是火災。下面兩行給「方法或數據成員找不到」,在上面TestClassEvent例如:

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

我沒有測試以下,因爲我不太清楚如何:

腳本控制無法處理來自一類的事件在同一 項目爲應用程序它是被託管在 https://diigo.com/08we68

不知道下面是相關的,d on't很理解它: http://www.programmersheaven.com/discussion/79452/me-activecontrol-and-events

+0

也許,添加對象只需添加該對象而不需要該對象的任何代碼。就像您將oClass添加爲對象一樣,但未添加其代碼。這是因爲我們有Set objModule = .Modules.Add(「NewModule」),後來我們使用objModule.AddCode向它添加代碼。所以建議添加一個類,然後添加代碼爲「Private WithEvents frm As New UserForm」,「Private Sub frm_Click()」&_ vbNewLine&vbTab&「MsgBox」「Hello,world from added module。」「 「&_ vbNewLine&」End Sub「 –

+0

hi @MukulVarshney,thx for suggestion。但是,在將對象添加到ScriptControl後,我不知道如何向對象添加代碼。你說要添加一個模塊,但我如何將添加的模塊與添加的對象關聯?你能顯示代碼嗎? -thx –

回答

1

以使其成爲工作的重點是:必須後設置的監聽級的事件觸發對象都添加到腳本Control--不是之前。這意味着,這條線一定的SC裏面執行:

Set oListener.EventFiringObject = oEventFiringObject

這裏燒製的工作示例和腳本控制內部對象之間的響應,事件。

在這個例子中:

  • 我演示2種事件射擊的對象:一個自定義類,以及一個工作表。
  • 自定義類在添加到scriptcontrol(「sc」)之前被實例化。
  • 我在自定義sc對象中調用方法。

要設置演示

  • 啓動一個新的項目(即在添加新工作簿)。
  • 在您的VB IDE中,添加對Microsoft Script Control的引用。
  • 創建以下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 

測試

通過Main()步驟。當Raise_MyEvent火災發生時,您會看到MyEventclsEventClass,clsClassListener用消息框響應該事件。

現在切換到Excel前端,並在Sheet2的單元格中輸入一個值。你會看到clsSheetListener回覆Change事件與消息框。

+0

接下來的目標是實例化腳本控制中的偵聽器和/或事件源... –

相關問題