【问题标题】:Using IE.Document.getElementById to auto fill webforms使用 IE.Document.getElementById 自动填充网络表单
【发布时间】:2018-08-03 02:22:17
【问题描述】:

我无法让 VBA 从 Excel 电子表格自动填充网络表单。这是我的html代码。

<form action="/my-handling-form-page" method="post">
<br>
  <div>
  1&emsp;
  <label for="part1">&emsp;</label>
  <input type="text" id="part1_id" name="part1_name">
  <label for="action1">&emsp;</label>
  <input type="text" id="action1_id" name="action1_name">
  <label for="quanity1">&emsp;</label>
  <input type="text" id="quanity1_id" name="quanity1_name">
  <label for="description1">&emsp;</label>
  <input type="text" id="description1_id" name="description1_name">
  </div>
</form>

这是我的 VBA 代码

Sub FillInternetForm()
  Dim IE As Object
  'create new instance of IE. use reference to return current IE if
  'you want to use open IE window. Easiest way I know of is via title bar
  Set IE = CreateObject("InternetExplorer.Application")
  'Go to web page listed insde quotes
  IE.Navigate "file:///C:/Users/stren/Documents/Sample_WebForm.html"
  'Show the window
  IE.Visible = True
  'Wait until IE is done loading page
  While IE.busy
    DoEvents
  Wend
  'Fill the field with data
    IE.Document.getElementById("part1_name").Value = ThisWorkbook.Sheets("sheet1").Range("a1")
End Sub

当我运行 VBA 代码时,会弹出 webform,但第一个框中没有填写任何数据,并且出现错误:

Run-time error '-2147467259(80004005)':
Method 'Document' of object 'IWebBrowser2' failed

你知道这段代码有什么问题吗?

【问题讨论】:

  • 查看我对ie.Busy 用法的评论:stackoverflow.com/a/48490013/5781745 看看是否能解决您的问题
  • 在 Sub 的最后一行打断,运行代码,稍等片刻,然后点击继续。还是报错吗?
  • 这行怎么可能是有效的.getElementById("part1_name")?你在哪里找到这个part1_name 作为ID?试试这个 .getElementById("part1_id")?

标签: excel vba webforms getelementbyid


【解决方案1】:

我通过将 html 代码从我的笔记本电脑移动到网站解决了这个问题。当代码在我的笔记本电脑本地时,VBA 和 html 之间的握手似乎不起作用。谢谢大家的建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2019-03-28
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多