Search code examples
excelvbagetelementsbytagname

Passing HTML Elements to Procedures (as parameter)


I would like to call a procedure and pass some HTML elements to it.

If I call procedure this way SimpleSearch (eNames) then the For Each statement returns error

Object doesn't support this property or method

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

Solution

  • Remove the parenthesis from the procedure call:

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

    In VBA functions use parenthesis but procedures don't:

    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