0

I am attempting to create a bridge between VBA UserForm and a webbrowser control on the form. I don't like the limitations of VBA to query the HTML document or implement changes on the fly and would like to use Javascript instead.

So far I have tried:

Private Sub CommandButton2_Click()

Dim head As HTMLGenericElement
Dim scriptEl As HTMLGenericElement
Dim element As IHTMLScriptElement

Set head = WebBrowser1.Document.GetElementsByTagName("head")(0)
Set scriptEl = WebBrowser1.Document.createElement("script")

        scriptEl.Text = "function sayHello() { alert('hello') }"

        head.appendChild (scriptEl)
        WebBrowser1.Document.InvokeScript ("sayHello")
End Sub

I get the error 424 - object required at the "head.appendChild" line.

I have used IsObject() on both head, and scriptEl, both return true. So I totally confused!

If anyone could provide some guidance on what is happening that would be fantastic, or how to adapt what I have so far to carry scripts through the bridge like:

 WebBrowser1.Document.InvokeScript ("my javascript code that I wish to run here")
user1088793
  • 623
  • 2
  • 9
  • 19

1 Answers1

1
head.appendChild (scriptEl)

Here the parentheses around scriptEl are causing it to be evaluated as an expression (and the result of that evaluation is likely a non-object type.

head.appendChild scriptEl

Should work fine.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125