2

我正在开发一个 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,进入功能中指定的网页,但是当我尝试搜索特定标签时,它似乎失败了。我在搜索 Microsoft VBA Internet Explorer 文档时遇到了麻烦,并且知道哪些变量和方法可用于此任务?

4

3 回答 3

2

我建议设置对 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
于 2012-07-05T00:51:01.173 回答
2

团队,IE网页完整加载检查使用下面提到的代码

Application.Wait (Now + TimeValue("0:00:1"))

Do Until IE.Busy = False
   DoEvents
Loop
于 2017-05-17T09:54:56.283 回答
0
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
于 2020-06-11T03:49:00.520 回答