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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16660789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:12:26  来源:igfitidea点击:

Excel VBA script to prefill online form using IE?

excelvbaexcel-vbagetelementbyid

提问by user2300923

I am in need of assistance. I am trying to write a VBA script that would take the value in column A and place it on an online form in an input element with no ID but the name ("OldUrl"). Then the VBA script would take the value in the adjacent cell in column B and place that in the same form ("digiSHOP") in the input field named ("NewUrl").

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

The form is on a secure server however I have gotten as far as the window pulling up and the form selected. I am having trouble finding a way to target the input field since they have no ID. Below is my code and thank you for your help.

该表单位于安全服务器上,但是我已经得到了拉起窗口并选择了表单的程度。我很难找到定位输入字段的方法,因为它们没有 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

Also I wasn't sure how to target the entire column and loop it therefore the Value is set to the cell A2. This was more to test the script.

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

采纳答案by Tim Williams

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