使用 Excel VBA 阅读网页

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

Reading Web Pages using Excel VBA

excelvbaexcel-vbaexcel-2007webpage

提问by user618677

I want to read web pages using Excel VBA. How do I carry out this task? Is it even possible?

我想使用 Excel VBA 阅读网页。我如何执行此任务?甚至有可能吗?

回答by MikeD

Coming from Excel 2003, yes this is possible - you may use the SHDocVw.InternetExplorerand MSHTML.HTMLDocumentobjects to call a web page, gain control over and interact with the DOM object. After creating references to Microsoft HTML Object Library (...\system32\MSHTML.TLB) and Microsoft Internet Control (...\system32\ieframe.dll) you can play with the following example:

来自 Excel 2003,是的,这是可能的 - 您可以使用SHDocVw.InternetExplorerMSHTML.HTMLDocument对象来调用网页,获得对 DOM 对象的控制和交互。创建对 Microsoft HTML 对象库 (...\system32\MSHTML.TLB) 和 Microsoft Internet Control (...\system32\ieframe.dll) 的引用后,您可以使用以下示例:

Sub Test()
Dim Browser As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument

    Set Browser = New SHDocVw.InternetExplorer                     ' create a browser
    Browser.Visible = True                                         ' make it visible
    Application.StatusBar = ".... opening page"
    Browser.navigate "http://www.google.com"                       ' navigate to page
    WaitForBrowser Browser, 10                                     ' wait for completion or timeout

    Application.StatusBar = "gaining control over DOM object"
    Set HTMLDoc = Browser.document                                 ' load the DOM object
    WaitForBrowser Browser, 10

    ' at this point you can start working with the DOM object. Usefull functions are
    ' With HTMLDoc
    '     .getElementByID(string)
    '     .getElementsByTagName(string)
    '     .getElementsByName(string)
    '     .getAttribute(string)
    '     .setAttribute string, string     .... to change field values, click a button etc.
    ' End With

    Application.StatusBar = ""                                      ' time to clean up
    Browser.Quit
    Set HTMLDoc = Nothing
    Set Browser = Nothing
End Sub

Sub WaitForBrowser(Browser As SHDocVw.InternetExplorer, Optional TimeOut As Single = 10)
Dim MyTime As Single

    MyTime = Timer
    Do While Browser.Busy Or (Timer <= MyTime + TimeOut)
        DoEvents
    Loop

    ' if browser still busy after timeout, give up
    If Browser.Busy Then
        MsgBox "I waited for " & Timer - MyTime & " seconds, but browser still busy" & vbCrLf & _
               "I give up now!"
        End
    End If
End Sub

回答by Tim Williams

You can use VBA to automate IE (plenty of examples via Google) or you can fetch the page directly using an instance of MSHTTP (ditto plenty of examples on the web). Which is best for your needs will depend on exactly what you want to do. Hard to say more without more detailed requirements.

您可以使用 VBA 来自动化 IE(通过 Google 提供大量示例),或者您可以使用 MSHTTP 实例直接获取页面(网络上的大量示例也是如此)。哪种最适合您的需求将取决于您想要做什么。没有更详细的要求很难说更多。