是否可以(在 Microsoft Access 中)将 VBA 事件处理程序附加到 Web 浏览器控件的 HTML 事件

问题描述 投票:0回答:1

我有一个带有 WebBrowser 控件的访问表单 (b)。我通过以下方式“写”b 的内容:

    Dim h as String

    h = "<html><body>"
    h = h & "<form id='bf' ...>"
    h = h & "<input type='text' id='test' name='test'>"
    h = h & "<input type='submit' value='Submit'>"
    h = h & "</form>"
    h = h & "</body></html>"
    Call Me.b.Object.Document.Write(h)

我想通过 VBA 函数处理用户的响应(“提交”)。 在我的 VBA 代码中,可以通过

bf
访问 HTML 表单
Me.b.Object.Document.getElementById("bf")
,即一个
MSHTML.HtmlFormElement
并公开带有两个参数的
attachEvent
方法:

Function attachEvent(event As String, pdisp As object) As Boolean

我应该如何创建一个

pdisp
对象来完成以下操作?

    Call Me.b.Object.Document.getElementById("f").attachEvent("onsubmit", *???*)

提前感谢您对此问题的任何见解或对我的问题的任何替代解决方案。

Address Of
提供“函数指针”是行不通的。我认为所需的对象应该是某个实现某些接口的
WithEvents
类的实例...但是找不到有关该主题的任何文档。

尽管有 .Net,VBA MSHTML 库不提供对 ObjectForScripting 的访问...

vba ms-access event-handling dom-events webbrowser-control
1个回答
0
投票

使用带有

WithEvents
的类的示例:

类模块

clsHtmlInput
:

Option Explicit

Private WithEvents m_btn As MSHTML.HTMLInputElement
Private m_txt As MSHTML.HTMLInputElement

Public Sub SetInputs(theButton, theTextBox)
    Set m_btn = theButton
    Set m_txt = theTextBox
End Sub

Private Function m_btn_onclick() As Boolean
    MsgBox "Clicked: " & m_txt.Value
End Function

您的用户表单:

Dim o As clsHtmlInput '<< instance of your "withEvents" class

Private Sub UserForm_Activate()

    Dim el As MSHTML.HTMLInputElement, h As String
    With Me.wb1
        .navigate "about:blank"
        WaitFor wb1
        .document.Open "text/html"
        h = "<html><body>"
        h = h & "<input type='text' id='test' name='test'>"
        h = h & "<input type='submit' value='Submit' id='btnSubmit'>"
        h = h & "</body></html>"
        .document.Write h
        .document.Close
        WaitFor wb1

         .document.getElementById ("btnSubmit")

        Set o = New clsHtmlInput
        o.SetInputs .document.getElementById("btnSubmit"), _
                    .document.getElementById("test")

    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
© www.soinside.com 2019 - 2024. All rights reserved.