Excel VBA脚本使用IE预填充在线表单?

时间:2013-05-21 01:33:49

标签: excel vba excel-vba getelementbyid

我需要帮助。我正在尝试编写一个VBA脚本,该脚本将获取A列中的值并将其放在输入元素中的在线表单上,该输入元素没有ID但名称(“OldUrl”)。然后,VBA脚本将获取B列中相邻单元格中的值,并将其放在名为(“NewUrl”)的输入字段中的相同格式(“digiSHOP”)中。

表单位于安全的服务器上但是我已经到窗口拉起并选择了表单。我找不到一种方法来定位输入字段,因为它们没有ID。以下是我的代码,谢谢你的帮助。

Sub Redirect()
Dim IE As Object
Dim doc As Object
Dim form As Object
Dim OldURL As Object
Dim NewURL As Object

Set IE = CreateObject("InternetExplorer.Application")

With IE
    .Visible = True
    .Navigate "https://...."

    Do Until .ReadyState = 4: DoEvents: Loop

    Set doc = IE.Document
    Set form = doc.forms("digiSHOP")
    Set OldURL = doc.getElementById("OldUrl")'Error occurs here. Element has no ID
    OldURL.Value = Range("A2")
    Set NewURL = doc.getElementById("NewUrl")
    NewURL.Value = Range("B2")
    form.submit

    Do Until .ReadyState = 4: DoEvents: Loop
    Do While .Busy: DoEvents: Loop

    End With
End Sub

此外,我不确定如何定位整个列并循环它,因此将值设置为单元格A2。这更像是测试脚本。

1 个答案:

答案 0 :(得分:1)

Sub Redirect()
Dim IE As Object
Dim doc As Object

    Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Visible = True
        .Navigate "https://...."

        Do Until .ReadyState = 4: DoEvents: Loop

        With .Document.forms("digiSHOP")
            .elements("OldUrl").Value = Range("A2")
            .elements("NewUrl").Value = Range("B2")
            .submit
        End With

        Do Until .ReadyState = 4: DoEvents: Loop
        Do While .Busy: DoEvents: Loop

    End With
End Sub