使用vba

时间:2018-12-25 16:49:44

标签: excel vba

我正在尝试使代码正常工作,但是我无法使它单击一个sumbit按钮,因为Web在其ID中没有该按钮,以下是我尝试单击的html行,我如何单击它通过vba代码按钮。

 <button type="submit" class="btn btn-default" value="Login" 
 name="action">Submit</button>

1 个答案:

答案 0 :(得分:1)

您需要引用“ Microsoft Internet Controls”,它实际上是从VBA控制Internet Explorer。如果您可以给我网站地址,我可以为您提供更具体的帮助,但是如果它不能为您解决问题,这应该可以帮助您入门:

Public Sub ButtonClicker()
    Dim IE As InternetExplorer
    Dim website_address As String: website_address = "YOUR ADDRESS GOES HERE"
    Dim button_name As String: button_name = "action"
    Dim button_class As String: button_class = "btn btn-default"
    Dim open_in_IE As Boolean: open_in_IE = False  'Change to True if you want to see it in action
    Dim button_collection As Object
    Dim button As Object

    On Error GoTo error_handler

    If IE Is Nothing Then Set IE = New InternetExplorer
        With IE
            .Navigate website_address

            Do While .Busy
                DoEvents
            Loop

            Do While .ReadyState <> 4
                DoEvents
            Loop

            .Visible = open_in_IE
        End With

        Set button_collection = IE.Document.getElementsByName(button_name)
            Set button = button_collection(button_class)  'Inspect Locals here to see what comes through
                button.Click
            Set button = Nothing
        Set button_collection = Nothing

        IE.Quit
        Set IE = Nothing
    Exit Sub

error_handler:
    Debug.Print "IE Error: " & Err.Number & " - " & Err.Description
    If Not (IE Is Nothing) Then
        IE.Quit
        Set IE = Nothing
    End If
End Sub