Dear Experts,
I'm always running into this issue with websites that drives me insane. I use VBA to automate IE, and the current system I'm using is IE11 but sometimes in the office we also use as low as IE9.
My goal is very simple. Anytime there is a textbox which has an event that is triggered by the keyup, keydown or keypress events, and I need to simulate a keypress. Often, that is the only way to make the controls accept the values or even fire the events.
Currently, I've gotten around this by using extremely complicated windows api, where I grab the handle to the ie window, bring it to the front, set the focus on the textbox, and use a class I wrote which is better than sendkeys but does the same thing, in order to send the enter key to it. This sucks though because any programs I do this with have to run in the foreground with the focus and you can't do anything else while it's running and automating IE.
What I would love to do is either of these two approaches, but I can't figure either one out.
Using the .fireevent function on a htmlinputelement, for example, to send the enter key to the htmlelement.
Or somehow fire a java function which creates the keyboard event and sends it to the field in question so it's like I hit the enter key in it.
I'd really like to know how to use the .fireevent() func to do it, but I haven't been able to find any documentation on the web anywhere for the second argument.
Here's the definition from the vba's object browser fro the FireEvent func..
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
--
Function FireEvent(bstrEventName As String, [pvarEventObject]) As Boolean
Member of MSHTML.HTMLInputElement
So in order to make it work I gotta do something like inputElem.FireEvent("onkey
press", <something which is set to the keycode I want to fire>)
I created a test webpage so you guys can try out your script on it and see if you can get me a working example.. Thanks so much!
Here's the url of my test page..
http://www.iondataexpress.com/test/testkeypress.htm
Thank you again Experts!
Jeffrey
Public Sub TestIT()
'myWeb is a class which wraps all the funcs on shdocvw.internetexplorer that I use
'Just ignore this code, but realize it's creating a new shdocvw.internetexplorer,
' navigating to the test page, and then waiting for it to finish loading
myWeb.initMe_MainExplorer
myWeb.NavigateMe 1, "http://www.iondataexpress.com/test/testkeypress.htm"
myWeb.WaitForMe 1
Dim htmlDoc As HTMLDocument
Set htmlDoc = myWeb.GetWebDocument(1)
'Relevant code starts Here:
Dim inputElem As HTMLInputElement, pEvent As IHTMLEventObj
Set inputElem = htmlDoc.getElementById("tx
inputElem.Value = "hi"
Set pEvent = htmlDoc.CreateEventObject(
pEvent.keyCode = 13
Debug.Print inputElem.FireEvent("onkey
Stop
End Sub