vba 填充 Internet Explorer 输入框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27483919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:13:31  来源:igfitidea点击:

Filling Internet Explorer inputbox

vbainternet-explorerexcel-vbainputboxexcel

提问by Noldor130884

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.

问题很简单:在打开的 IE 页面上填充输入框。

Result: the code gets stuck on the line with getelementbyidshowing runtime error 424 (object required).

结果:代码卡在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 libraries were naturally imported (otherwise the "internetexplorer.application" wouldn't work.

Internet Explorer 库是自然导入的(否则“internetexplorer.application”将无法工作。

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.

我很确定我想要填写的字段称为“Nachnamevalue”,因为我今天早上在互联网上看到的内容。

The html code of my webpage (only the interesting piece) looks like this:

我的网页的 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">&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.

还有(我不知道它是否有帮助)一个“嵌入式”javascript,每次在“Nachnamevalue”输入框中写入至少 2 个字符时都会显示搜索结果。

What am I doing wrong?

我究竟做错了什么?

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

编辑:当我尝试逐步执行 Sub 时,我得到以下信息:

Set Doc = ie.document

Set Doc = ie.document

? Doc [object HTMLDocument] ( in the watchlist it is an object without any variables inside )

? Doc [object HTMLDocument] (在监视列表中它是一个内部没有任何变量的对象)

回答by Alex K.

GetElementByIdgets an element by its idattribute, but "Nachnamevalue"is the value of the nameattribute.

GetElementById通过其id属性获取元素,但"Nachnamevalue"是属性的值name

To use the name:

要使用名称:

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

回答by dee

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

这对我有用。该代码使用您在 file 中的问题的 HTML 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").

要检查存在于 momonet 中的所有输入元素的名称,您可以在可以使用的页面上执行 VBA 代码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

回答by genespos

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