Excel VBA create an embedded WebBrowser and use it

China☆狼群 提交于 2019-11-30 14:24:32
myWebBrowser.Object.Navigate "http://www.google.com"

more complete example:

Sub AddWebBroswerToWorksheet()

    Dim myWebBrowser
    Dim wb, doc, x As Long

    Sheet2.Activate
    Sheet1.OLEObjects(1).Delete

    Set myWebBrowser = Sheet1.OLEObjects.Add(ClassType:="Shell.Explorer.2", _
                       Left:=147, Top:=60.75, Width:=400, Height:=400)

    Set wb = myWebBrowser.Object
    With wb
        .Navigate "about:blank"
        .Document.Open "text/html"
        For x = 1 To 100
        .Document.write "hello world<br>"
        Next x
        .Document.Close
        .Document.body.Scroll = "no"
        Debug.Print .Document.body.innerHTML
    End With
    Sheet1.Activate 'switching back to the sheet seems to 
    '               '   trigger the display of the object

End Sub
noseratio

You need to pump Windows messages inside your WebBrowser.ReadyState <> READYSTATE_COMPLETE loop for this to work. Calling DoEvents/Sleep inside the loops does that, but has its own implications. Check these answers for further details and sample code:

https://stackoverflow.com/a/19019200/1768303

https://stackoverflow.com/a/19308865/1768303

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!