【发布时间】:2015-02-13 12:59:55
【问题描述】:
我阅读了很多关于我的问题的答案,但不知何故,如果我试图“模仿”我所看到的,我仍然无法做我需要做的事情。
问题很简单:在打开的 IE 页面上填充一个输入框。
结果:代码卡在getelementbyid 的行上,显示运行时错误 424(需要对象)。
Private Sub AddInfoFromIntranet()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
Application.SendKeys "{ESC}" ' I need this to ignore a prompt
With ie
.Visible = True
.navigate "{here goes the address of my website}"
Do Until Not .Busy And .readyState = 4
DoEvents
Loop
.document.getelementbyid("Nachnamevalue").Value = "{here goes whar I want to insert}"
End With
Set ie = Nothing
End Sub
Internet Explorer 库是自然导入的(否则“internetexplorer.application”将不起作用。
我很肯定我想填写的字段称为“Nachnamevalue”,这是我今天早上浏览互联网时了解到的。
我的网页的 html 代码(仅有趣的部分)如下所示:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
'{here there are info on the style, which i'm gonna ignore}
</style>
</head>
<body bgcolor="#ffffcc"><table width="1000"><tbody><tr><td>
<form name="Suchform" action="index.cfm" method="get" target="bottom_window">
Nachname:
<select name="Nachnamepulldown" class="font09px" onchange="wait_and_search()">
<option value="BEGINS_WITH">beginnt mit
<option value="EQUAL">ist
<option value="CONTAINS">enthält
</option></select>
<input name="Nachnamevalue" onkeyup="wait_and_search()" type="text" size="8">
Abteilung:
<select name="Abteilungpulldown" class="font09px" onchange="wait_and_search()">
<option value="BEGINS_WITH">beginnt mit
<option value="EQUAL">ist
<option value="CONTAINS">enthält
</option></select>
<input name="Abteilungvalue" onkeyup="wait_and_search()" type="text" size="3">
<input name="fuseaction" type="hidden" value="StdSearchResult">
<input type="submit" value="suchen">
<script language="JavaScript" type="text/JavaScript">
document.Suchform.Nachnamevalue.focus();
</script>
</form>
</td></tr></tbody></table></body>
</html>
还有(我不知道它是否有帮助)一个“嵌入式”javascript,每次在“Nachnamevalue”输入框中写入至少 2 个字符时,都会显示搜索结果。
我做错了什么?
编辑: 当我尝试逐步执行 Sub 时,我得到以下信息:
Set Doc = ie.document
?文档 [对象 HTMLDocument] (在监视列表中它是一个内部没有任何变量的对象)
【问题讨论】:
-
你应该用这个
.document.getElementsByName("Nachnamevalue").Value = "{here goes whar I want to insert}"替换这个:.document.getelementbyid("Nachnamevalue").Value = "{here goes whar I want to insert}"。即使我认为,如果您已经测试了@Alex 的答案但没有成功,问题是当您指向对象时页面尚未完全加载。尝试使用Application.Wait方法强制等待 5 秒,看看是否有效。 -
我试过
Application.Wait (Now + TimeValue("0:00:05"))... 不起作用 -
您能否尝试先写
Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 5),然后在引发异常的行之前写MsgBox .document.getElementsByName("Nachnamevalue"),然后告诉我们您看到了什么? -
什么都没有,因为我得到运行时错误 438 对象不支持此属性或方法。它指的是 .getElementsByName
-
好的,先试试
Set obj = .document.getElementsByName("Nachnamevalue"),然后再试试MsgBox obj,然后等待重试。对于您提供的 HTML,它不可能不起作用:您的 HTML 代码/VBA 代码有拼写错误,或者您不在With ie块内。
标签: vba internet-explorer excel inputbox