excel - VBA 循环 : Automate web form filling

标签 excel vba loops automation

我的目标是使用存储在 excel 表中的动态数据填写 web 表单。
该过程应如下所示:

  • 一个。
  • 转到存储在“A”中的链接我
  • 使用从 "A"i 到 "G"i
  • 的数据填写 Web 表单中的给定字段
  • 点击“保存”
  • 等待 5 秒
  • 用文本填充列“H”i:“已创建”
  • 重复下一行 (i+1) 直到最后一行
  • 完成后(在最后一行之后),msgbox:“进程完成”

  • Below is my version without a loop that works but only for B2:G2 Row.


    Sub copy_project_loop()
    
    Dim IE As Object
    Dim Doc As HTMLDocument
    Set IE = CreateObject("InternetExplorer.Application")
    Dim SHELL_OBJECT
    SHELL_OBJECT = "WScript.Shell"
    Set objShell = CreateObject(SHELL_OBJECT)
    
    
    IE.Visible = True
    IE.navigate "URL link"
    
        Do While IE.Busy
             Application.Wait DateAdd("s", 1, Now)
        Loop
        Set Doc = IE.document
    
    Doc.getElementById("Project|Name").Value = ThisWorkbook.Sheets("data").Range("B2").Value
    Doc.getElementById("customer|UserDefinedIdTA").Value = ThisWorkbook.Sheets("data").Range("C2").Value
        Doc.getElementById("customer|UserDefinedIdTA").Focus
            objShell.SendKeys "{Enter}"
                Application.Wait DateAdd("s", 2, Now)
    Doc.getElementById("selEngagement").Value = ThisWorkbook.Sheets("data").Range("D2").Value
    Doc.getElementById("txtPlannedStart").Value = ThisWorkbook.Sheets("data").Range("E2").Value
    Doc.getElementById("Project|ProjTimeAppTA").Value = ThisWorkbook.Sheets("data").Range("F2").Value
    Doc.getElementById("Project|SecondProjTimeAppTA").Value = ThisWorkbook.Sheets("data").Range("G2").Value
        Application.Wait DateAdd("s", 5, Now)
    Doc.getElementById("Button1").Click
    
    End Sub
    

    请协助我为此任务定义一个循环。

    谢谢你的支持,
    斯拉瓦。

    最佳答案

    对于任何可能觉得它有用的人,下面是一个很好的例子,说明如何循环存储在表格中的动态数据范围并用它填充 Web 表单。
    非常感谢 Kamolga 帮助我完成了这项精彩的任务。

    Sub DynamicWebPage()
    
    ' here I define elemnts for the loop
    Dim sht As Worksheet
    Set sht = ThisWorkbook.Sheets("data")
    Dim LastRow As Long
    Dim i As Long
    LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    
    ' here I define Internet Explorer
    Dim IE As Object
    Dim Doc As HTMLDocument
    Set IE = CreateObject("InternetExplorer.Application")
    
    ' here I define Object to sendkeys
    Dim SHELL_OBJECT
    SHELL_OBJECT = "WScript.Shell"
    Set objShell = CreateObject(SHELL_OBJECT)
    
    ' here I define range for the loop
    For i = 2 To LastRow
    
    ' here I ask the Internet explorer to be visable & Navigate to value in cell "H"i
    IE.Visible = True
    IE.navigate sht.Range("H" & i).Value
    
    ' here I ask the Internet explorer to wait few seconds
    Do While IE.Busy
        Application.Wait DateAdd("s", 5, Now)
    Loop
    
    Set Doc = IE.document
    
    '******* From here the macro fill the data from excel table in range into WEB elements******
    Doc.getElementById("Project|Name").Value = sht.Range("A" & i).Value
    Doc.getElementById("customer|UserDefinedIdTA").Value = sht.Range("B" & i).Value
    Application.Wait DateAdd("s", 1, Now)
    Doc.getElementById("customer|UserDefinedIdTA").Focus
    objShell.SendKeys "{Enter}"
    Application.Wait DateAdd("s", 5, Now)
    Doc.getElementById("selEngagement").Value = sht.Range("C" & i).Value
    'date format should be m/d/yyyy
    Doc.getElementById("txtPlannedStart").Value = sht.Range("D" & i).Value
    Doc.getElementById("Project|ProjTimeAppTA").Value = sht.Range("E" & i).Value
    Doc.getElementById("Project|SecondProjTimeAppTA").Value = sht.Range("F" & i).Value
    Application.Wait DateAdd("s", 5, Now)
    Doc.getElementById("Button1").Click
    
    ' here I ask to populate column "G"i with "created" string to know that this raw was successfully done
    sht.Range("G" & i).Value = "Created"
    
    Next i
    MsgBox "Process 100% completed"
    
    End Sub
    

    关于excel - VBA 循环 : Automate web form filling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53495733/

    相关文章:

    python - 使用 python 中 pandas 的 read_excel 函数将日期保留为字符串

    vba - 从用户窗体监视类事件

    关闭Excel时VBA密码提示

    javascript - 在 VBA 中从 Javascript 获取 XmlHttpRequest 的 ReadyState

    php - wordpress循环调整大小

    python - 如何使用Python迭代Mysql数据库查询?

    ms-access - VBA检查对象是否已设置

    vba - 用于恢复保存后更改的宏

    excel - 如何使用 VBA 获取前五篇新闻文章的名称和链接

    c# - 我如何总结每 5 个数字而不是所有数字?