我正在研究一个VBA函数来转到网页,找到一个HTML元素,并显示其内容。这是我到目前为止所拥有的。
Function WebTableToSheet(webpage As String, tag As String, count As Integer) As String
'Tested using IE7, Excel 2000 SP1, and Windows XP
Dim objIE As Object
Dim varTables, varTable, document
Dim allTags, tags
Dim varRows, varRow
Dim varCells, varCell
Dim lngRow As Long, lngColumn As Long
Dim strBuffer As String
Set objIE = CreateObject("InternetExplorer.Application")
'Setup
With objIE
.AddressBar = False
.StatusBar = False
.MenuBar = False
.Toolbar = 0
.Visible = True
.Navigate webpage
End With
'Load webpage
While objIE.Busy
Wend
While objIE.document.ReadyState <> "complete"
Wend
varTable = objIE.document.All
allTags = objIE.document.All.tags(tag)
WebTableToSheet = allTags.Item(count)
Debug.Print "Testing function"
Cleanup:
Set varCell = Nothing: Set varCells = Nothing
Set varRow = Nothing: Set varRows = Nothing
Set varTable = Nothing: Set varTables = Nothing
objIE.Quit
Set objIE = Nothing
End Function
我能够打开InternetExplorer,到达函数中指定的网页,但是当我尝试搜索特定标签时,它似乎失败了。我在搜索 VBA Internet Explorer 文档时遇到困难Microsoft并且知道哪些变量和方法可用于此任务?
我建议设置对shdocvw.dll和Microsoft HTML对象库的引用。在XP shdocvw中.dll在你的system32文件夹中,HTML对象库应该在你的引用列表中。我目前只能访问XP,所以你必须搜索你正在使用的任何其他Windows版本。然后在代码中执行以下操作:
Dim IE as SHDocVw.InternetExplorer
Dim doc as HTMLDocument
Dim el as IHTMLElement
Set IE = new SHDocVw.InternetExplorer
With IE
.navigate "some.webpage.com"
.visible = true
End With
Do
'Nothing
Loop Until IE.readyState = READYSTATE_COMPLETE ' = 4
Set doc = IE.Document
Set el = doc.getElementById("someElementId")
If Not el Is Nothing Then
MsgBox el.innerText
End If
团队,IE网页完全加载检查使用以下提到的代码
Application.Wait (Now + TimeValue("0:00:1"))
Do Until IE.Busy = False
DoEvents
Loop
ieApp.Navigate "Https://duckduckgo.com/"
'Wait for Internet Explorer to finish loading
Do While ieApp.Busy: DoEvents: Loop
Do While ieApp.Busy And Not ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
ieApp.Document.getelementbyid("search_form_input_homepage").Value = "Gajendra Santosh"
ieApp.Document.getelementbyid("search_button_homepage").Click