我在 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 需要时间。
结果:
关于excel - getElementById 在 VBA 中不起作用,错误 438,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56590814/