Filling Internet Explorer inputbox

南楼画角 提交于 2020-01-21 11:33:06

问题


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 )


回答1:


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"



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/27483919/filling-internet-explorer-inputbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!