I have an Access Form with a WebBrowser Control (b). I "write" b's content on the fly by:
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)
I want to process the user's response (to "Submit") by a VBA function.
In my VBA code, the HTML Form bf
can be accessed through Me.b.Object.Document.getElementById("bf")
, that is an MSHTML.HtmlFormElement
and exposes an attachEvent
method with two araguments:
Function attachEvent(event As String, pdisp As object) As Boolean
How should I create a pdisp
Object to complete the following?
Call Me.b.Object.Document.getElementById("f").attachEvent("onsubmit", *???*)
Thanks in advance for any insight on this or any alternate solution to my problem.
Providing a "function pointer" with Address Of
doesn't wotk. I presume that the Object required should be an instance of some WithEvents
Class implmenting some interface... But Couldn't find any documentation on the subject.
Despite .Net, VBA MSHTML Library doesn't provide access to ObjectForScripting...
Example of using a class with WithEvents
:
Class module 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
Your userform:
Dim eventHandler 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
Set eventHandler = New clsHtmlInput
eventHandler.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
If you're familiar with HTML and javascripting (or at least, the subset of "modern" HTML/js which is still supported in the older web browser control...), this can be a way to create interfaces which go beyond the basic VBA form controls.