簡體   English   中英

Excel VBA控件IE

[英]Excel VBA control IE

我在WiseOwl網站上找到了以下代碼。 我正在嘗試找到一個有效的示例(以供學習),以了解如何在Google搜索字段中輸入搜索字符串。 我不斷收到調試錯誤462,並且f無法通過。 導致問題的行是:

  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

任何幫助表示贊賞。

謝謝

我通常不遍歷頁面上的所有元素,而是遍歷頁面上的所有元素,然后按其名稱查找它。 它運行速度較慢,但​​是可以完成工作。

請嘗試以下操作:

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