简体   繁体   English

Excel VBA控件IE

[英]Excel VBA control IE

I have found the following code on the WiseOwl website. 我在WiseOwl网站上找到了以下代码。 I am trying to find a working example (to learn from) on how to enter a seach string into Googles search field. 我正在尝试找到一个有效的示例(以供学习),以了解如何在Google搜索字段中输入搜索字符串。 I keep getting a debug error 462 andf cant get past this. 我不断收到调试错误462,并且f无法通过。 The line causing the problem is: 导致问题的行是:

  ieApp.Document.getElementById("gbqfqw").Value = "Excel VBA"

My total code is: 我的总代码是:

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

Any help is appreciated. 任何帮助表示赞赏。

Thanks 谢谢

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. 它运行速度较慢,但​​是可以完成工作。

Try the following: 请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM