【问题标题】:VBA to Enter Data Online and Submit FormVBA 在线输入数据并提交表单
【发布时间】:2013-07-01 16:46:32
【问题描述】:

我正在尝试使用 VBA 将数据从 Excel 提交到在线网络表单。问题发生在我尝试选择和更新表单的 * 区域内。我尝试了各种选项(按 ID、名称等获取元素),但均未成功。我认为这个问题与确定适当的元素有关。我已经看到有关在 VBA 中使用 Locals 功能的建议,但我不确定如何将其用于此目的。我感觉有经验的人可以通过查看网站上的源代码、在 VBA 中使用 Locals 或其他一些技术来很快解决这个问题。

表单设置为所有字段均为文本,我可以毫无问题地在线输入/提交数据。

提前感谢任何帮助/建议。

Dim IE As Object

Sub submitFeedback3()
Application.ScreenUpdating = False

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://spreadsheetbootcamp.com/excel-efficiency-trainer-feedback/"

Application.StatusBar = "Submitting"
  ' Wait while IE loading...
 While IE.Busy
 DoEvents
 Wend
 **********************************************************************
 IE.Document.getElementById("Form_Attempts-1372643500")(0).Value = "1"
 IE.Document.getElementById("submit-1-1372643500")(0).Click
 **********************************************************************
 Application.StatusBar = "Form Submitted"
 IE.Quit
 Set IE = Nothing

Application.ScreenUpdating = True
End Sub

【问题讨论】:

  • 那个页面在 IE8 中完全崩溃了
  • 可以输入数据/提交吗?这是这个网页的唯一目的。
  • 我看到的只是十几个未标记的文本框和一个“提交”按钮。不过看看 HTML,你最好尝试根据它们的名称(似乎是恒定的)填充文本框,而不是它们的 Id(它似乎有一个不断变化的数字后缀)
  • 很好地了解不断变化的 ID。我假设数字部分是静态的。根据这个线程,看起来使用 getelementsbyname 可能实际上想要查找 id?: stackoverflow.com/questions/14575671/… 。我将尝试更改一些 html 编码。
  • IE.Document.formNameHere.inputNameHere.Value 应该可以工作。

标签: vba excel


【解决方案1】:

试试下面的代码

Dim IE As Object

Sub submitFeedback3()
    Application.ScreenUpdating = False

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "http://spreadsheetbootcamp.com/excel-efficiency-trainer-feedback/"

    Application.StatusBar = "Submitting"
    ' Wait while IE loading...
    While IE.Busy
        DoEvents
    Wend
    ' **********************************************************************
    delay 5
    IE.Document.getElementById("experience-1372700847").Value = "ddddd1"
    delay 5
    IE.Document.getElementById("Form_Time_Best-1372700847").Value = "ddddddddddd2"
    delay 5
    IE.Document.getElementById("submit-1-1372700847").Click
    '**********************************************************************
    Application.StatusBar = "Form Submitted"
    IE.Quit
    Set IE = Nothing

    Application.ScreenUpdating = True
End Sub

Private Sub delay(seconds As Long)
    Dim endTime As Date
    endTime = DateAdd("s", seconds, Now())
    Do While Now() < endTime
        DoEvents
    Loop
End Sub

【讨论】:

  • 我对表单使用了不同的设置,并让一切正常工作。谢谢大家的建议。
  • 我使用的是相同的代码。最后,它单击一个按钮导航到新页面。完成后,我无法访问新页面的元素。我可能会丢失什么。
  • @Richa 您应该确保您的页面已完成加载。我创建了一个简单的 sub 来帮助实现这一点:Sub IEBusy(IE_Object As Object)Do While IE_Object.Busy or IE_Object.ReadyState &lt;&gt; 4DoEventsLoopExit Sub。然后使用Call IEBusy(IE),等待页面完成加载就这么简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 2018-09-23
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
相关资源
最近更新 更多