简体   繁体   English

如何将JavaScript结果从本地文件发送到VBA Webbrowser控件

[英]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: 我有两个问题:

  1. 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?

  2. 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.

相关问题 MFC的WebBrowser控件—如何注入Java脚本? - WebBrowser Control from MFC — How to Inject Javascript? 如何将本地脚本文件添加到WebBrowser控件的HTML中? - How do I add a local script file to the HTML of a WebBrowser control? JavaScript文件的本地路径在加载到WebBrowser控件本地HTML页面下无法正常工作 - Local path to JavaScript file are not working under loaded to WebBrowser control local HTML page 在托管WebBrowser控件的应用程序中,如何在由WebBrowser控件查看的页面中调用JavaScript函数? - How might I call a JavaScript Function in a Page Viewed by a WebBrowser Control from the Application Hosting the WebBrowser Control? 如何在 WebBrowser 控件中注入 Javascript? - How to inject Javascript in WebBrowser control? 在这种情况下如何从C#调用WebBrowser控件中的JavaScript - how to Calling JavaScript in a WebBrowser control from C# in such a situation 如何从WPF WebBrowser控件调试页面的JavaScript代码 - How to debug JavaScript code of a page from WPF WebBrowser Control Windows Phone WebBrowser中的Javascript参考本地文件 - Javascript reference local file in Windows Phone WebBrowser 从WebBrowser控件中删除Javascript / ChildElement - Remove Javascript/ChildElement from WebBrowser Control 尝试使用VBA在webbrowser控件上执行javascript命令 - Trying to use VBA to execute a javascript command on a webbrowser control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM