我正在尝试在 VBA 用户窗体和表单上的 Web 浏览器控件之间创建一座桥梁。我不喜欢 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”行需要对象。
我在 head 和 scriptEl 上都使用了 IsObject(),都返回 true。所以我完全困惑了!
如果任何人都可以提供一些关于正在发生的事情的指导,那就太棒了,或者如何调整我迄今为止所拥有的内容以通过桥传输脚本,例如:
WebBrowser1.Document.InvokeScript ("my javascript code that I wish to run here")
最佳答案
head.appendChild (scriptEl)
这里 scriptEl
周围的括号导致它被评估为表达式(并且该评估的结果可能是非对象类型。
head.appendChild scriptEl
应该可以正常工作。
关于javascript - VBA - IsObject(myobject) 返回 True,但是出现 "object required"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54061121/