Filling the Internet Explorer Input Window

I read so many answers to my problem, but for some reason, if I try to "imitate" what I see, I am still not able to do what I need.

The problem is very simple: fill in the input field on the open IE page.

Result: the code loops on the line with getelementbyid , displaying a 424 runtime error (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 "internetexplorer.application" will not work.

I am sure that the field I want to fill out is called “Nachnamevalue”, as from what I learned this morning, looking around the internet.

The html code of my webpage (just an interesting snippet) 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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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) a “built-in” javascript that brings search results every time at least two characters are written in the “Nachnamevalue” input field.

What am I doing wrong?

EDIT: When I try to execute Sub step by step, I get the following:

Set Doc = ie.document

? doctor [HTMLDocument object] (on the watchlist, this is an object without any variables inside)

+5
source share
3 answers

GetElementById receives the element by id property, but "Nachnamevalue" is the value of the name attribute.

To use the name:

 .document.Forms("Suchform").Elements("Nachnamevalue").value = "xxx" 
+7
source

It worked for me. The code uses the HTML from your question in c:\Temp\page1.html .

 Option Explicit ' Add reference to Microsoft Internet Controls ' Add reference to Microsoft HTML Object Library Sub AddInfoFromIntranet() Dim ie As SHDocVw.InternetExplorer Dim doc As MSHTML.HTMLDocument Dim elements As MSHTML.IHTMLElementCollection Dim nachnameValueInput As MSHTML.HTMLInputElement Set ie = New SHDocVw.InternetExplorer With ie .Visible = True .navigate "c:\Temp\page1.html" Do Until Not .Busy And .readyState = 4 DoEvents Loop Set doc = .document Set elements = doc.getElementsByName("Nachnamevalue") If Not elements Is Nothing Then Set nachnameValueInput = elements(0) If Not nachnameValueInput Is Nothing Then _ nachnameValueInput.Value = "{here goes whar I want to insert}" End If .Quit End With Set ie = Nothing End Sub 

To check the names of all input elements that exist on the coin, you execute VBA code on a page that you can use getElementsByTagName("input") .

  Set elements = doc.getElementsByTagName("input") If Not elements Is Nothing Then Dim inputElement For Each inputElement In elements Debug.Print inputElement.Name Next inputElement End If 
+1
source

You can try to use all the input data and select one of them that you need:

 Set Elements = IE.document.getelementsbytagname("Input") For Each Element In Elements If Element.Name = "Nachnamevalue" Then Element.Value = {Here your value} Exit For End If Next Element 
0
source

Source: https://habr.com/ru/post/1209094/


All Articles