嗨,我正在尝试在电子表格中动态创建一个 Web 浏览器,然后使用它,但 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(几乎在那里):
现在我需要一种方法来删除工作表2.激活工作表1.激活
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
myWebBrowser.Object.Navigate "http://www.google.com"
更完整的示例:
Sub AddWebBrowserToWorksheet()
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
将 Windows 消息泵入WebBrowser.ReadyState <> READYSTATE_COMPLETE
循环才能正常工作。在循环中调用DoEvents
/Sleep
可以做到这一点,但有其自身的含义。有关更多详细信息和示例代码,请查看以下答案:
https://stackoverflow.com/a/19019200/1768303
https://stackoverflow.com/a/19308865/1768303