Excel VBA创build一个embedded的WebBrowser并使用它

嗨我试图dynamic创build一个电子表格内的Web浏览器,然后使用它,但WebBrowserfunction似乎不工作

这是我如何创buildWebBrowser

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 

 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 

您需要在WebBrowser.ReadyState <> READYSTATE_COMPLETE循环内抽取Windows消息以使其正常工作。 在循环中调用DoEvents / Sleep DoEvents ,但有其自身的含义。 检查这些答案的进一步细节和示例代码:

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

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

Interesting Posts