We help IT Professionals succeed at work.
Get Started

VBA Use FireEvent or something to fire KeyPress event to send Enter Key to input[type='text']

11,964 Views
Last Modified: 2016-02-11
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("onkeypress", <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
Comment
Watch Question
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2017
Commented:
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE