6

您好我正在尝试在电子表格中动态创建一个 Web 浏览器,然后使用它,但 WebBrowser 功能似乎不起作用

这是我创建 WebBrowser 的方法

Set myWebBrowser = Sheets("test").OLEObjects.Add(ClassType:="Shell.Explorer.2", Link:=False, DisplayAsIcon:=False, left:=147, top:=60.75, width:=141, height:=96)

这将起作用

myWebBrowser.top = 10

但这会给我一个错误

myWebBrowser.Navigate ("about:blank")

关于我应该做什么的任何想法谢谢

更新:

这也不起作用并给出错误:

myWebBrowser.Object.Document.body.Scroll = "no"
myWebBrowser.Object.Silent = True
myWebBrowser.Object.Navigate ("about:blank")
While myWebBrowser.Object.ReadyState <> READYSTATE_COMPLETE
    Application.Wait (Now + TimeValue("0:00:01"))
Wend
myWebBrowser.Object.Refresh

更新2(几乎在那里):

现在我需要一种方法来删除Sheet2.Activate Sheet1.Activate

Sheet2.Activate
Sheet1.Activate

Set wb = myWebBrowser.Object

With wb
    .Silent = True
    .Navigate "about:blank"
    Do While .ReadyState <> READYSTATE_COMPLETE
        Application.Wait (Now + TimeValue("0:00:01"))
    Loop
    .Document.Open "text/html"
    Do While .ReadyState <> READYSTATE_COMPLETE
        Application.Wait (Now + TimeValue("0:00:01"))
    Loop
    .Document.write html
    .Document.Close
    .Document.body.Scroll = "no"
    .Refresh
    Debug.Print .Document.body.innerHTML
End With
4

2 回答 2

7
myWebBrowser.Object.Navigate "http://www.google.com"

更完整的例子:

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
于 2013-10-25T22:58:22.143 回答
0

您需要在WebBrowser.ReadyState <> READYSTATE_COMPLETE循环中泵入 Windows 消息才能使其正常工作。在循环内调用DoEvents/Sleep可以做到这一点,但有其自身的含义。检查这些答案以获取更多详细信息和示例代码:

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

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

于 2013-10-26T01:16:05.730 回答