2012-10-13 119 views
0

在VBA中可以拦截一个javascript onchange事件并在web浏览器控件中获取源ID#可能拦截change事件吗?

<select name="status0" id="status0" onchange="javascriptonchangeeventhere"> 

上面的代码是一个组合框演示,如果我要从组合框中选择一个项目,它会在javascript中处理onchange。它所做的是拉起一个包含当前日期和参考号的表单,而不是我想拉起我自己的用户表单,然后将这些数据吐到相关的(notes0)字段中。如何在发生这种情况时拦截它,以便将其分配给正确的字段。

+0

有没有实际的方法来做到这一点?甚至确定一个onchange事件发生在哪里? –

回答

2

下面是一个简短的例子。项目引用的 “Microsoft HTML对象库” 和 “Microsoft Internet控制”

类模块中设置 “clsEvents”:

Option Explicit 

Public WithEvents slct As MSHTML.HTMLSelectElement 
Public WithEvents href As MSHTML.HTMLAnchorElement 

'Note that having defined "href" as "WithEvents", if you choose "href" 
' from the left-hand dropdown at the top of the class code module 
' then the right-hand dropdown will populate with events you can select 
' from to create an event-handling procedure in the class 

Private Function href_onclick() As Boolean 
    Debug.Print "link clicked" 
    href_onclick = False 'cancels the navigation 
End Function 

Private Sub slct_onchange() 
    Debug.Print "select onchange - value is " & slct.Value 
End Sub 

Private Function slct_onclick() As Boolean 
    Debug.Print "select onclick - value is " & slct.Value 
End Function 

在常规模块:

Option Explicit 

Dim evt As clsEvents 

Sub Setup() 

    Dim IE As New InternetExplorer 
    Dim el As Object, el2 As Object 

    Set evt = New clsEvents 

    IE.Visible = True 
    IE.navigate "http://www.csee.wvu.edu/~riggs/html/select_example.html" 

    Do While IE.Busy 
    Loop 

    Set el = IE.document.getElementsByTagName("select")(1) 
    Set el2 = IE.document.getElementsByTagName("a")(1) 

    If Not el Is Nothing Then 
     Debug.Print "setting event capture: currentvalue=" & el.Value 
     Set evt.slct = el 
    End If 

    If Not el2 Is Nothing Then 
     Debug.Print "setting event capture on link:" & el2.innerText 
     Set evt.href = el2 
    End If 

End Sub 

如果运行Setup子然后更改第二个选择的值或单击IE中的页面上的“Javascript”链接,您应该在VB编辑器的立即窗口中看到输出。

希望能帮助你开始。

+0

非常感谢你! –

+0

+1很好做! –