Filling Internet Explorer inputbox
Asked Answered
I

3

5

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">&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) 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 )

Idiomorphic answered 15/12, 2014 at 12:17 Comment(11)
You should replace this: .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 the Application.Wait method and see if it works.Hendrickson
I tried with Application.Wait (Now + TimeValue("0:00:05"))... Doesn't workIdiomorphic
Can you try to write first Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 5) and then MsgBox .document.getElementsByName("Nachnamevalue") just before the line raising the exception, and tell us what you see?Hendrickson
Nothing because I get runtime error 438 object doesn't support this property or method. It refers to the .getElementsByNameIdiomorphic
Ok, try rather Set obj = .document.getElementsByName("Nachnamevalue") and then MsgBox 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 the With ie block.Hendrickson
Doesn't work... T_T the html is that one, I'm totally sure... And I really AM inside the with ie ... Could it be that since the tag is inside a <form> I have to address the form before?Idiomorphic
Not really. Names are usually uniques and if you're sure about the HTML, the object is clearly there with that name. So if you're unable to find it, the only two things that I can tell you are: 1) add the Microsoft Internet Controls reference, if you didn't yet; 2) Try to extend the time waiting for the page loading, maybe execute the whole code in debug, step by step, and re-execute the same instructions a couple of times manually. It's really really strange that this doesn't work.Hendrickson
I am working with the internet controls and I debugged the code. I also wrote ? ie.busy and it gave back "false"...Idiomorphic
Ok, let's try further. First of all, separate the processes. 1) Open the IE browser with With ie .navigate "http:\\www. .Visible = True End With. Hence, put a waiter for being sure the page is correctly loaded (don't use ie.Busy because its status could change before). So put Application.Wait TimeSerial() as I wrote you some comments ago. Once this is done: Set allInputs = ie.document.getElementsByTagName("input") [to be continued]Hendrickson
Technically, in allInputs 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 name Nachnamevalue.Hendrickson
Let us continue this discussion in chat.Idiomorphic
M
7

GetElementById gets an element by its id attribute, but "Nachnamevalue" is the value of the name attribute.

To use the name:

.document.Forms("Suchform").Elements("Nachnamevalue").value = "xxx"
Mattheus answered 15/12, 2014 at 12:26 Comment(1)
Tried this too... Always same error shows up :( Is it possible that I didn't load some libraries? I have the "HTML Object Library" loaded and I tried to load all that I could find, but nothing... Still stuck on that line...Idiomorphic
P
1

This worked for me. The code uses HTML from your question in file 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 which exist at the momonet you execute the VBA code on the page you could 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
Prepossessing answered 15/12, 2014 at 13:44 Comment(8)
In this case elements(0) is blank and Not nachnameValueInput Is Nothing = FalseIdiomorphic
Then the element name 'Nachnamevalue' does not exist on the page. Could the name be misspelled? It might be 'NachnameValue' instead? Use F12 and checkt it.Prepossessing
I copy-pasted the code from the webpage... Can't be that is misspelled. @dee: I tried what you suggested in your edit... debug.print produces nothingIdiomorphic
Is the page public? Could you share the link so i can try by myself? Have you tryid to press F12 after the page is loaded and check if the input box is there?Prepossessing
As the name of the Sub suggests... Sadly no. It's intranet. I'm trying to get an intranet webpage search the name I'm entering in that inputboxIdiomorphic
msdn.microsoft.com/en-us/library/ie/gg589512%28v=vs.85%29.aspx ... check if the page is rendered with the names you expect it to be.Prepossessing
Let us continue this discussion in chat.Prepossessing
I open the page with F12 then select the field I want to fill with the tool... the html code in my question is what I gotIdiomorphic
C
0

You can try cycling all input and selecting the one is named as 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
Cabinda answered 15/12, 2014 at 18:40 Comment(3)
it's strange. The code perfectly worked for me. Have you changed {Here your value} with "Antony" (or anythin else) ?Cabinda
Have you analyzed the code (by step with F8)? what's wrong: catching of the element or assigning of the value?Cabinda
Yes I did. Please have a look here #27507205 tooIdiomorphic

© 2022 - 2024 — McMap. All rights reserved.