Цикл VBA: автоматизировать заполнение веб-форм - PullRequest
0 голосов
/ 27 ноября 2018

Моя цель - заполнить веб-формы, используя динамические данные, которые хранятся в таблице Excel.Процесс должен выглядеть следующим образом:

  1. A.
    1. Перейти к ссылке, хранящейся в «A» i
    2. Заполнить указанные поля в веб-форме, используя данные от «A» i до «G» i
    3. Нажмите «Сохранить»
    4. Подождите 5 секунд
    5. Заполните столбец "H" i текстом: "Создано"
    6. Повторяйте следующую строку (i + 1) до последней строки
    7. После завершения (после последней строки), msgbox: «процесс завершен»

Ниже приведена моя версия без цикла, который работает, но только для B2: строка G2.

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

Пожалуйста, помогите мне определить цикл для этой задачи.

Спасибо за вашу поддержку, Слава.

1 Ответ

0 голосов
/ 27 ноября 2018

Для тех, кто может найти это полезным, ниже приведен отличный пример того, как зацикливать динамический диапазон данных, хранящихся в таблице, и заполнять их веб-формой.Большое спасибо Камолге, которая помогла мне с этим замечательным заданием.

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...