【发布时间】:2018-08-03 02:22:17
【问题描述】:
我无法让 VBA 从 Excel 电子表格自动填充网络表单。这是我的html代码。
<form action="/my-handling-form-page" method="post">
<br>
<div>
1 
<label for="part1"> </label>
<input type="text" id="part1_id" name="part1_name">
<label for="action1"> </label>
<input type="text" id="action1_id" name="action1_name">
<label for="quanity1"> </label>
<input type="text" id="quanity1_id" name="quanity1_name">
<label for="description1"> </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