Excel VBA creates an embedded WebBrowser and uses it

Hey. I am trying to dynamically create a web browser inside a spreadsheet and then use it, but the WebBrowser functions do not work

This is how I create WebBrowser

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

It will work

myWebBrowser.top = 10

But that will give me an error

myWebBrowser.Navigate ("about:blank")

Any ideas on what I should do, thanks

UPDATE:

This also will not work and will 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):

Now I need a way to remove 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
source share
2 answers
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
+6
source

Windows WebBrowser.ReadyState <> READYSTATE_COMPLETE, . DoEvents/Sleep , . :

fooobar.com/questions/1172904/...

fooobar.com/questions/1509055/...

0

All Articles