Excel VBA 创建一个嵌入式 WebBrowser 并使用它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19600586/
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
Excel VBA create an embedded WebBrowser and use it
提问by oMG
Hi I'm trying to dynamically create a web browser inside a spreadsheet and then use it but the WebBrowser functions don't seem to work
嗨,我正在尝试在电子表格中动态创建 Web 浏览器,然后使用它,但 WebBrowser 功能似乎不起作用
Here is how I create the 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)
This will work
这将工作
myWebBrowser.top = 10
But this will give me an error
但这会给我一个错误
myWebBrowser.Navigate ("about:blank")
Any ideas on what should I do thank you
关于我该怎么做的任何想法谢谢
UPDATE:
更新:
This will also don't work and give an error:
这也将不起作用并给出错误:
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
UPDATE 2 (almost there):
更新 2(几乎在那里):
Now I need a way to remove the Sheet2.Activate Sheet1.Activate
现在我需要一种方法来删除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
回答by Tim Williams
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
回答by 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:
您需要在WebBrowser.ReadyState <> READYSTATE_COMPLETE
循环中泵入 Windows 消息才能使其正常工作。在循环内调用DoEvents
/Sleep
可以做到这一点,但有其自身的含义。检查这些答案以获取更多详细信息和示例代码:
https://stackoverflow.com/a/19019200/1768303
https://stackoverflow.com/a/19019200/1768303