excel - getElementById 在 VBA 中不起作用,错误 438

标签 excel vba getelementbyid

我在 VBA 方面相对较新(我知道基础知识,但不多),我正在尝试设置一些代码来为我填写在线表格,但是当我运行我的代码时出现 438 错误:

object doesnt support this property or method



当它到达时
ie.document.getElementById ("q")

我已将 HTML 对象库和 Microsoft Internet 控件添加到我的引用中。我看过很多在线论坛。我什至将整个脚本直接复制并粘贴到 VBA 中。没有什么可以使用getElementById() .这是我的代码:
Sub internetstuff()

Dim ie As Object

Set ie = CreateObject("internetexplorer.application")

ie.Visible = True

ie.navigate ("https://www.google.com/")

Set searchbx = ie.document.getElementById("q")

searchbx.Value = "Howdy!"

End Sub

应该发生的是,它应该打开 InternetExplorer,转到 Google,然后在搜索栏中填充“你好!”。
相反,它只打开谷歌,然后我收到错误消息并停止运行。

最佳答案

因为"q"不是 ID ,这是一个 Name .

您必须使用 GetElementsByName并选择第一个元素

尝试这个:

Sub internetstuff()

Dim ie As Object

Set ie = CreateObject("internetexplorer.application")

ie.Visible = True

ie.Navigate ("https://www.google.com/")

    Do Until ie.ReadyState >= 4
        DoEvents
    Loop


Set searchbx = ie.document.getElementsByName("q")(0)

searchbx.Value = "Howdy!"

End Sub

我还添加了一个等待事件,以防加载 Google 需要时间。

结果:

enter image description here

关于excel - getElementById 在 VBA 中不起作用,错误 438,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56590814/

相关文章:

excel - 如何在 VBA 中格式化特定的邮件值

excel - 无法在 Excel IDE 中设置对 MS Word 的引用

数组循环内的 Javascript 价格和数量调整

javascript - GetElementByID 行为古怪

java - Apache POI - 提取列中的所有不同值

Excel 将一个单元格中的多个值与一列中的值列表相匹配,然后接收单个总和?

string - 对于每个控制变量必须是变量或对象

excel - Countif A 或 B 但没有重复计算

excel - 当行数 = 0 时删除 Excel 列中的单元格

Javascript:循环到任何带有偶数/奇数描述的数字。为什么它只记录用户输入?