使用IE.Document.getElementById自动填充webforms

时间:2018-02-23 03:59:11

标签: excel vba webforms getelementbyid

我很难让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

任何想法这个代码有什么问题?

1 个答案:

答案 0 :(得分:0)

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