[英]How to sending JavaScript results from local file to VBA Webbrowser control
I'm using the standard webbrowser control in MS Access. 我在MS Access中使用标准的Web浏览器控件。 The control shows a local HTML file.
该控件显示一个本地HTML文件。 Now, I want to send data from HTML to VBA.
现在,我想将数据从HTML发送到VBA。
<input type="text" onchange="foo(this.value)">
How to send data to VBA? 如何将数据发送到VBA? I have two problems:
我有两个问题:
If the HTML file is local, I found no solution to start JavaScript at all. 如果HTML文件是本地文件,则根本找不到启动JavaScript的解决方案。 If the file has an http URI,
alert()
for example is possible, but not if the file is local. 如果文件具有http URI,则例如可以使用
alert()
,但是如果文件是本地文件则不能。 How can I use JavaScript in a local file? 如何在本地文件中使用JavaScript?
How can I send the result of a JavaScript function to VBA? 如何将JavaScript函数的结果发送到VBA?
PS.: I'm not searching how to start Javascript from VBA (Webbrowser.Document.parentwindow.execscript) PS .:我没有搜索如何从VBA启动Javascript(Webbrowser.Document.parentwindow.execscript)
Thx 谢谢
Martin 马丁
You can set up js-to-VBA communication using simple classes implementing WithEvents to hook up VBA references to elements in your hosted HTML page. 您可以使用实现WithEvents的简单类来设置js到VBA的通信,以将VBA引用连接到托管HTML页面中的元素。
When the example below is run, editing and then clicking out of the HTML textbox (so firing the onchange
event) will trigger a VBA messagebox via the class field linked to the input. 运行下面的示例时,编辑然后单击HTML文本框之外的框(这样会触发
onchange
事件)将通过链接到输入的类字段触发VBA消息框。
To find out how to fix your issues with local pages and js, Google "mark of the web". 要了解如何解决本地页面和js的问题,请使用Google的“网络标记”。
In class module clsHtmlText
: 在类模块
clsHtmlText
:
Option Explicit
Private WithEvents txt As MSHTML.HTMLInputElement
Public Sub SetText(el)
Set txt = el
End Sub
Private Function txt_onchange() As Boolean
MsgBox "changed: " & txt.value
End Function
In a UserForm with an embedded browser control wb1
: 在具有嵌入式浏览器控件
wb1
:
Option Explicit
Dim o As clsHtmlText '<< instance of our "withEvents" class
Private Sub UserForm_Activate()
Dim el As MSHTML.HTMLInputElement
With Me.wb1
.Navigate "about:blank"
WaitFor wb1
.Document.Open "text/html"
'or you can load a page from a URL/file
'Note: local pages need "mark of the web" in the markup
.Document.write "<html><input type='text' size=10 id='txtHere'></html>"
.Document.Close
WaitFor wb1
Set el = .Document.getelementbyId("txtHere")
Set o = New clsHtmlText
o.SetText el '<< assign the textbox so we can monitor for change events
End With
End Sub
'utility sub to ensure page is loaded and ready
Sub WaitFor(IE)
Do While IE.ReadyState < 4 Or IE.Busy
DoEvents
Loop
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.