使用excelvba阅读网页

使用excelvba阅读网页,vba,excel,excel-2007,webpage,Vba,Excel,Excel 2007,Webpage,我想使用Excel VBA阅读网页。我如何执行这项任务?甚至有可能吗?您可以使用VBA自动化IE(通过谷歌提供大量示例),或者您可以使用MSHTTP实例直接获取页面(与web上的大量示例相同)。哪个最适合你的需要取决于你到底想做什么。如果没有更详细的要求,很难说更多。来自Excel 2003,是的,这是可能的-您可以使用SHDocVw.InternetExplorer和MSHTML.HTMLDocument对象来调用网页,控制DOM对象并与之交互。创建对Microsoft HTML对象库(…\s

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

您可以使用VBA自动化IE(通过谷歌提供大量示例),或者您可以使用MSHTTP实例直接获取页面(与web上的大量示例相同)。哪个最适合你的需要取决于你到底想做什么。如果没有更详细的要求,很难说更多。

来自Excel 2003,是的,这是可能的-您可以使用
SHDocVw.InternetExplorer
MSHTML.HTMLDocument
对象来调用网页,控制DOM对象并与之交互。创建对Microsoft HTML对象库(…\system32\MSHTML.TLB)和Microsoft Internet控件(…\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
子测试()
将浏览器设置为SHDocVw.InternetExplorer
将HTMLDoc设置为MSHTML.HTMLDocument
Set Browser=New SHDocVw.InternetExplorer'创建浏览器
Browser.Visible=True“使其可见”
Application.StatusBar=“…打开页面”
Browser.navigate“http://www.google.com“'导航到第页
WaitForBrowser浏览器,10'等待完成或超时
Application.StatusBar=“获得对DOM对象的控制”
设置HTMLDoc=Browser.document'加载DOM对象
WaitForBrowser浏览器,10
'此时,您可以开始使用DOM对象。有用的函数是
“使用HTMLDoc
'.getElementByID(字符串)
'.getElementsByTagName(字符串)
'.getElementsByName(字符串)
'.getAttribute(字符串)
'.setAttribute字符串,字符串。。。。要更改字段值,请单击按钮等。
"以
Application.StatusBar=“”清理时间
浏览器,退出
设置HTMLDoc=Nothing
设置浏览器=无
端接头
子WaitForBrowser(浏览器为SHDocVw.InternetExplorer,可选超时为Single=10)
把我的时间当作单身
我的时间=计时器

在浏览时执行。忙碌或(计时器)我在谷歌上搜索了很多次,我确实得到了一些有用的东西。可能是我的视力问题,但我不介意你的一些链接,例如,请指出你使用的版本!我已切换到2007版非常感谢,这太棒了>