Excel VBA控件IE [英] Excel VBA control IE
本文介绍了Excel VBA控件IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在WiseOwl网站上找到了以下代码.我正在尝试找到一个有效的示例(以供学习),以了解如何在Google搜索字段中输入搜索字符串.我不断收到调试错误462,并且f无法通过.导致问题的行是:
I have found the following code on the WiseOwl website. I am trying to find a working example (to learn from) on how to enter a seach string into Googles search field. I keep getting a debug error 462 andf cant get past this. The line causing the problem is:
ieApp.Document.getElementById("gbqfqw").Value = "Excel VBA"
我的总代码为:
Sub FillInBBCSearchForm()
Dim ieApp As New SHDocVw.InternetExplorer
ieApp.Visible = True
'go to the website of interest
ieApp.Navigate "http://www.google.co.uk"
Do While ieApp.Busy
DoEvents
Loop
'wait for page to finish loading
Do While ieApp.Busy And Not ieApp.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
'fill in the search form
ieApp.Document.getElementById("gbqfqw").Value = "Excel" 'DEBUG ERROR HERE
'wait for page to finish loading
Do While ieApp.Busy And Not ieApp.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
End Sub
感谢您的帮助.
谢谢
推荐答案
我通常不遍历页面上的所有元素,而是通过其名称查找它,而不是通过其ID来引用该元素.它的运行速度稍慢,但是可以完成工作.
Instead of referring to an element by its ID, I usually loop through all elements on the page, and find it by its name. It runs a bit slower, but gets the job done.
请尝试以下操作:
Sub FillInBBCSearchForm()
Dim ieApp As New SHDocVw.InternetExplorer
'go to the website of interest
ieApp.Navigate "http://www.google.co.uk"
'wait for page to finish loading
Do While ieApp.Busy: DoEvents: Loop
'it sometimes takes more than one wait loop for the page to load properly
Do While ieApp.Busy And Not ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'find the required field
Set doc = ieApp.Document.getElementsByTagName("input")
For Each lnk In doc
If lnk.Name = "q" Then
'input search text
lnk.Value = "Excel VBA"
End If
Next lnk
ieApp.Visible = True
AppActivate ieApp.Document.Title
End Sub
这篇关于Excel VBA控件IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文