I read so many answers to my problem but somehow if I try to "mimic" what I see, I still am not able to do what I need.
The problem is very simple: fill an inputbox on an opened IE page.
Result: the code gets stuck on the line with getelementbyid
showing runtime error 424 (object required).
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 libraries were naturally imported (otherwise the "internetexplorer.application" wouldn't work.
I am positive that the field I want to fill is called "Nachnamevalue" as from what I learned this morning taking a look around the internet.
The html code of my webpage (only the interesting piece) looks like this:
<!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>
There is also (I don't know if it can help) an "embedded" javascript that brings results of a search up every time at least 2 characters in the "Nachnamevalue" inputbox are written.
What am I doing wrong?
EDIT: When I try to execute the Sub step-by-step, I get the following:
Set Doc = ie.document
? Doc [object HTMLDocument] ( in the watchlist it is an object without any variables inside )
.document.getelementbyid("Nachnamevalue").Value = "{here goes whar I want to insert}"
with this.document.getElementsByName("Nachnamevalue").Value = "{here goes whar I want to insert}"
. Even if I think that, if you have already tested @Alex's answer with no success, the problem is that the page is not fully loaded yet when you point the object. Try to force waiting 5 seconds with theApplication.Wait
method and see if it works. – HendricksonApplication.Wait (Now + TimeValue("0:00:05"))
... Doesn't work – IdiomorphicApplication.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 5)
and thenMsgBox .document.getElementsByName("Nachnamevalue")
just before the line raising the exception, and tell us what you see? – HendricksonSet obj = .document.getElementsByName("Nachnamevalue")
and thenMsgBox obj
, and retry with the Wait. For the HTML you have provided, it's impossible that it doesn't work: either there's a spelling mistake on your HTML code/on your VBA code, or you're not inside theWith ie
block. – Hendricksonwith ie
... Could it be that since the tag is inside a <form> I have to address the form before? – IdiomorphicWith ie
.navigate "http:\\www.
.Visible = True
End With
. Hence, put a waiter for being sure the page is correctly loaded (don't useie.Busy
because its status could change before). So putApplication.Wait TimeSerial()
as I wrote you some comments ago. Once this is done:Set allInputs = ie.document.getElementsByTagName("input")
[to be continued] – HendricksonallInputs
you should have all the input elements of the page. Hence, write this:count = 1
-For Each obj In allInputs
-On Error Resume Next
-Range("A" & count) = obj.Name
-count = count + 1
Next obj
. In the spreadsheet, you should get all the names of the input elements you have got. Please tell us if in the list, it appears the nameNachnamevalue
. – Hendrickson