vba 从 HTML 复制数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18789316/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:18:54  来源:igfitidea点击:

Copy data from HTML

htmlexcelparsingvba

提问by user2774120

I am trying to learn how to parse data from HTML using Excel VBA. So I found one example online which works fine but when I change URL address from www.yahoo.comto local HTML file on C it gives me error i.e. Method 'busy' of object 'IwebBrowser2' failed.Code is:

我正在尝试学习如何使用 Excel VBA 从 HTML 解析数据。因此,我在网上找到了一个工作正常的示例,但是当我将 URL 地址从www.yahoo.comC 上的本地 HTML 文件更改为本地 HTML 文件时,它给了我错误,即对象“IwebBrowser2”的方法“忙”失败。代码是:

Sub GetBodyText()

Dim URL As String
Dim Data As String

URL = "file:///C:/test.html"

Dim ie As Object
Dim ieDoc As Object

Set ie = CreateObject("InternetExplorer.Application")
ie.navigate URL

Do Until (ie.readyState = 4 And Not ie.Busy)
    DoEvents
Loop

Set ieDoc = ie.Document

Data = ieDoc.body.innerText

'Split Data into separate lines
'or just use Range("A1")=data
Dim myarray As Variant
myarray = Split(Data, vbCrLf)

For i = 0 To UBound(myarray)
    'Start writing in cell A1
    Cells(i + 1, 1) = myarray(i)
Next


ie.Quit
Set ie = Nothing
Set ieDoc = Nothing

End Sub

回答by Joe

For IE, just use:

对于 IE,只需使用:

URL = "c:\test.html"