在哪里可以找到使用 Internet Explorer 的 VBA 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11334143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to find VBA functions for working with internet explorer
提问by user1207381
I am working on a VBA function to go to a webpage, find a single HTML element, and display its contents. Here is what I have so far.
我正在使用 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
I am able to open InternetExplorer, got to the webpage specified in the function, but when I try searching for a specific tag, it seems to fail. I have had trouble searching for Microsoft VBA Internet Explorer documentation and knew what variables and methods are available for this task?
我可以打开 InternetExplorer,进入函数中指定的网页,但是当我尝试搜索特定标签时,它似乎失败了。我在搜索 Microsoft VBA Internet Explorer 文档时遇到问题,并且知道哪些变量和方法可用于此任务?
回答by mkingston
I'd recommend setting a reference to shdocvw.dll and the Microsoft HTML Object Library. In XP shdocvw.dll is in your system32 folder, the HTML Object Library should be in your list of references. I only have access to XP at the moment, so you'll have to do a search for any other ver of windows you're using. Then in your code you can do the following:
我建议设置对 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
回答by Sathik Raja
Team, IE webpage full loading check use below mentioned code
团队,IE 网页满载检查使用下面提到的代码
Application.Wait (Now + TimeValue("0:00:1"))
Do Until IE.Busy = False
DoEvents
Loop