【问题标题】:Excel VBA script to prefill online form using IE?Excel VBA 脚本使用 IE 预填在线表格?
【发布时间】:2013-05-21 01:33:49
【问题描述】:

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

表单位于安全服务器上,但是我已经拉起窗口并选择了表单。我很难找到一种方法来定位输入字段,因为它们没有 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。这更多是为了测试脚本。

【问题讨论】:

  • 试试form.elements("OldUrl").Value = Range("A2")Value

标签: vba excel getelementbyid


【解决方案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

【讨论】:

  • 成功了,谢谢蒂姆。代码很容易阅读,现在我知道如何按名称定位元素。再次感谢!
猜你喜欢
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2014-08-25
  • 2013-05-12
  • 1970-01-01
相关资源
最近更新 更多