单击Web按钮的Excel VBA代码

时间:2018-01-22 09:21:47

标签: html vba excel-vba excel

需要帮助如何为此

创建excel vba代码

enter image description here

我需要这些代码才能完成我的宏。

提前致谢

3 个答案:

答案 0 :(得分:2)

  

首先,您需要创建一个对:

的引用      
      
  • Microsoft Internet Controls
  •   
  • Microsoft HTML对象库      
        

    在VBE中,点击工具>参考

      
  •   
  
Sub clickLink()

    Dim ie As New InternetExplorer, Url$, doc As HTMLDocument
    Url = "http://UrlToYourLink.com"

    With ie
        .navigate Url
        Do While .Busy Or .readyState < READYSTATE_COMPLETE
            DoEvents
        Loop
        doc = .document
        .Visible = True
    End With

    Dim myBtn As Object
    Set myBtn = doc.getElementsByClassName("button rounded")(0)
    myBtn.Click

End Sub

答案 1 :(得分:1)

Internet控件用于浏览网页,HTML对象用于标识用户名和密码文本框,并使用控制按钮提交文本。

Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()

Dim oHTML_Element As IHTMLElement
Dim sURL As String

On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

HTMLDoc.all.Email.Value = "sample@vbadud.com"
HTMLDoc.all.passwd.Value = "*****"

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub

该计划需要参考以下内容:

1 Microsoft Internet Controls
2. Microsoft HTML Object Library

答案 2 :(得分:1)

Microsoft Internet控件是一种很好的方法,但是如果不允许添加新的引用,这是另一种关于Web报废的方法。 这种方法不像微软互联网控件和HTML对象那样“干净”,但它完成了工作。

Sub GoogleSearch()
Dim ie As Object
Dim objSearchBnt As Object
Dim objCollection As Object
Dim i As Integer

    'initialize counter
    i = 0

    'Create InternetExplorer Object
    Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = True

    'navigate to the url
    ie.navigate "Www.google.com"

    'Statusbar shows in the buttom corner of excel
    Application.StatusBar = "Loading, please wait..."

    'Wait until page is ready
    Do While ie.busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    'Store all the elements with input tag
    Set objCollection = ie.Document.getElementsByTagName("input")

    'Go through all input elements
    While i < objCollection.Length
        'input search field
        If objCollection(i).Name = "q" Then
            objCollection(i).Value = "Hello World"
        End If
        'search button
        If objCollection(i).Type = "submit" Then
           Set objSearchBnt = objCollection(i)
        End If
        i = i + 1
    Wend

    objSearchBnt.Click

    'Clean up
    Set objSearchBnt = Nothing
    Set objCollection = Nothing
    Set ie = Nothing

    'Give excel control over the status bar agian
    Application.StatusBar = ""
End Sub