从早期绑定转换为后期绑定

时间:2016-08-08 17:24:06

标签: vba internet-explorer internet-explorer-11 late-binding

更新#2

我更改了txtbox和submitbtns的值(0)(也尝试了(1)),没有更改。

我还需要注意Button有不同的名称,我也在这里做了相应的更新。

Dim TBox As String          'Name of object textbox to have value changed in
Dim TBtn As String          'Name of object button to be pressed
    TBox = "masked1"
    TBtn = "button"

If Not IE Is Nothing Then
    Set txtBox = IE.Document.getElementsByClassName(TBox)(0)
    Set submitBtn = IE.Document.getElementsByClassName(TBtn)(0)

    txtBox.Value = tVal
    submitBtn.Click
End If

更新#1

因此,@cyboashu提供的建议看起来很有希望。但是,我仍然无法让我的txtbox更新为value = tVal(字符串)。

Dim oShell      As Object
Dim oWin        As Object
Dim IE          As Object
Dim lTotlWin    As Long
Dim lCtr

Debug.Print Time & " --- IE Objects & Values ---"       ' Debugger Section
Set oShell = CreateObject("Shell.Application")
    Debug.Print Time & " [obj ] oShell..: " & oShell    ' Debug oShell
Set oWin = oShell.Windows()
    Debug.Print Time & " [obj ] oWin....: " & oWin      ' Debug oWin

lTotlWin = oWin.Count - 1   '/ Starts with zero
Debug.Print Time & " [long] lTotlWin: " & lTotlWin      ' Debug lTotlWin

For lCtr = 0 To lTotlWin
    If UCase(oWin.Item(lCtr).FullName) Like "*IEXPLORE.EXE" Then
        Set IE = oWin.Item(lCtr)
    End If
Next
Debug.Print Time & " [obj ] IE......: " & IE
If Not IE Is Nothing Then
    MsgBox "Found and hooked!!"
End If

Dim TBox As String          'In the event the textbox's name changes for some reason
    TBox = "masked1"

If Not IE Is Nothing Then
    Set txtBox = IE.Document.getElementsByClassName(TBox)
    Debug.Print Time & " [obj ] txtbox..: " & txtbox
    Set submitBtn = IE.Document.getElementsByClassName(TBox)
    Debug.Print Time & " [obj ] submitBtn:" & submitBtn

    txtBox.Value = tVal
    submitBtn.Click
End If

Set shellwins = Nothing

Debug.Print Time & "- - - END SUB - - -" & E

End Sub

(调试器值,如果有人关心)..

2:44:11 PM --- IE Objects & Values ---
2:44:11 PM [long] lTotlWin: 5
2:44:11 PM [obj ] IE......: Internet Explorer
2:44:11 PM - - - END SUB - - -

1 个答案:

答案 0 :(得分:0)

获取对象在这里不起作用。

MS说的是什么:https://support.microsoft.com/en-gb/kb/239470

  

调用GetObject以在客户端系统上获取正在运行的ActiveX对象   将是一个很大的安全风险,因为任何运行对象   系统可以在没有用户直接许可的情况下访问,并且是   因此Internet Explorer不允许。没有办法改变   此行为,通过代码或由最终用户手动完成。

试试这个:

Sub testIELateBinding()

    Dim oShell              As Object
    Dim oWin                As Object
    Dim IE                  As Object
    Dim lTotlWin            As Long
    Dim lCtr


    Set oShell = CreateObject("Shell.Application")
    Set oWin = oShell.Windows()

    lTotlWin = oWin.Count - 1 '/ Starts with zero

    For lCtr = 0 To lTotlWin
        If UCase(oWin.Item(lCtr).FullName) Like "*IEXPLORE.EXE" Then
            Set IE = oWin.Item(lCtr)
        End If
    Next


    If Not IE Is Nothing Then
        MsgBox "Found and hooked!!"
    End If


End Sub