将HTML元素传递给过程(作为参数)

时间:2018-10-16 08:48:23

标签: excel vba excel-vba getelementsbytagname

我想调用一个过程并将一些HTML元素传递给它。

如果我以这种方式SimpleSearch (eNames)调用过程,那么For Each语句将返回错误

  

对象不支持此属性或方法

Sub Navigate()    
    Set eNames = IE.Document.getElementsByTagName("button")
    SimpleSearch (eNames)
End Sub


Sub SimpleSearch(ssitems)
    For Each ssitem In ssitems
        If ssitem.innerText = "Simple Search" Then
            ssitem.Focus
            ssitem.Click
            Exit For
        End If
    Next ssitem

    Do While IE.Busy
    Loop
    Do While IE.ReadyState < 4
    Loop
End Sub

1 个答案:

答案 0 :(得分:1)

从过程调用中删除括号:

Sub Navigate()
    Set eNames = IE.Document.getElementsByTagName("button")
    SimpleSearch eNames
End Sub

在VBA中,功能使用括号,但过程不使用括号:

Sub Test()
    MyString = MyFunction("parameter")   'function call with parenthesis
    MyProcedure "parameter"              'procedure call without parenthesis
End Sub


Function MyFunction(Parameter As String) As String
    'some code
    MyFunction = "Return Value"   'a function should return a value
End Function

Sub MyProcedure(Parameter As String)
    'some code
End Sub