如何使用Excel VBA自动化动态更改的网页URL和自动填充?

时间:2018-01-22 16:00:51

标签: excel excel-vba vba

如何使用Excel VBA自动化动态更改的网页网址和自动填充?

1 个答案:

答案 0 :(得分:0)

您的问题是关于细节的,但根据我的理解,您的代码正在运行,但您想要获取在代码完成后打开的新选项卡。

您可以使用我拥有的功能执行此操作并定期使用。这将查看打开的浏览器中的URL(浏览器是否隐藏),并允许您将对象声明设置为包含您的URL的选项卡。

  

警告:正如JohnRC在评论中所述,处理金融交易的网页的自动操作是不明智的。编码错误可能会产生意外结果。我个人不建议自动进行金融交易,而且您自行承担的任何建议都需要您自担风险。

功能:

Function GetIE(sLocation As String) As Object

    Dim objShell As Object, objShellWindows As Object, o As Object
    Dim sURL As String
    Dim RetVal As Object

    Set RetVal = Nothing
    Set objShell = CreateObject("shell.application")
    Set objShellWindows = objShell.Windows

    For Each o In objShellWindows
        sURL = ""
        On Error Resume Next
        'check the URL and if it's the one you want then
        'assign it to the return value and exit the loop
        sURL = o.document.Location
        On Error GoTo 0
        If sURL Like sLocation Then
            Set RetVal = o
            Exit For
        End If
    Next o

    Set GetIE = RetVal

End Function

在下面的代码中,您现在将ie2设置为新网页,其中包含网址字符串"*bildesk.com/id*"。完成此操作后,您可以像处理第一个ie2对象一样操纵IE

Sub SearchBot()

    'dimension (declare or set aside memory for) our variables
    Dim IE As InternetExplorer 'special object variable representing the IE browser
    Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
    Dim y As Integer 'integer variable we'll use as a counter
    Dim result As String 'string variable that will hold our result link

    'initiating a new instance of Internet Explorer and asigning it to objIE
    Set IE = New InternetExplorer

    Dim iedoc As Object

    'navigate IE to this web page (a pretty neat search engine really)
   IE.navigate "https://www.nbpdcl.co.in/(S(qq5avnlkl4xr2iqstldu0ehl))/frmQuickBillPaymentAll.aspx"

    'wait here a few seconds while the browser is busy
    Do Until IE.readyState = READYSTATE_COMPLETE: DoEvents: Loop

    'in the search box put cell "A2" value
    IE.document.getElementById("MainContent_txtCANO").Value = _
      Sheets("Sheet1").Range("A2").Value

    'click the 'go' button
    IE.document.getElementById("MainContent_btnSubmit").Click

  'wait here a few seconds while the browser is busy
    Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop

    'in the amount box put cell "B2" value
    IE.document.getElementById("MainContent_txtAmountPayable").Value = _
      Sheets("Sheet1").Range("B2").Value

      'in the EMAIL box put cell "C2" value
    IE.document.getElementById("txtEmailId").Value = _
      Sheets("Sheet1").Range("C2").Value

      'in the PHONE box put cell "D2" value
    IE.document.getElementById("txtMobileNo").Value = _
      Sheets("Sheet1").Range("D2").Value

       'click the 'verify' button
    IE.document.getElementById("MainContent_rbtnlstPaymode_0").Checked = True


      'click the 'verify' button
    IE.document.getElementById("MainContent_btnConfirmPay").Click

    'wait here a few seconds while the browser is busy
    Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop

    'click the 'verify' button
    IE.document.getElementById("MainContent_btnPayNow").Click

    '<--------- Use the function here ---------->

    Dim ie2 As InternetExplorer
    Set ie2 = GetIE("*bildesk.com/id*")


End Sub

因此,只需包含随模块提供的功能,您就应该进行设置。