我正在尝试在VBA UserForm和表单上的Webbrowser控件之间建立桥梁。我不喜欢VBA的局限性来查询HTML文档或即时执行更改,而是想使用Javascript。
到目前为止,我已经尝试过:
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
我收到错误424-“ head.appendChild”行所需的对象。
我在头和脚本El上都使用过IsObject(),它们都返回true。所以我完全糊涂了!
如果任何人都可以提供一些指导,以指导正在发生的事情,那将是很棒的事情,或者是如何适应我到目前为止所拥有的东西,以便像下面这样通过桥传送脚本:
WebBrowser1.Document.InvokeScript ("my javascript code that I wish to run here")
答案 0 :(得分:1)
head.appendChild (scriptEl)
scriptEl
周围的括号使它作为表达式求值(该求值的结果很可能是非对象类型。
head.appendChild scriptEl
应该工作正常。