MattB
MattB

Reputation: 2241

VBA Sending the Enter key to an HTMLInputElement

I have what I hope will be a simple question. I have a bit of code where I load a web page and enter a number into a field in IE. It is as follows:

Option Explicit

Private Sub GettingCashAvail()

Dim IE          As InternetExplorerMedium
Dim HTMLDoc     As HTMLDocument
Dim IEField     As HTMLInputElement

Set IE = New InternetExplorerMedium

IE.navigate "https://website.com/login.aspx"

While IE.Busy
    DoEvents
Wend

Do Until IE.statusText = "Done"
    DoEvents
Loop

Set HTMLDoc = IE.document

Set IEField = HTMLDoc.getElementById("ucGetAccountNumber_txtAccountNumber")

IEField.Value = "#########"

Now this field supports pressing enter to move to the next page. I could dim a second object for the submit button and then do something like IESubmit.Click to enter the information and navigate to the new screen, but what I'd like to do is somehow send the return key. Here is the portion of the source from the web page that I'm interested in:

onkeypress="return clickButton(event,'ucGetAccountNumber_btnSubmit')"

Do you think there is any way to simply send the enter key? Thanks for the ideas if so, if not I can just get that submit button set to a second input object.

Upvotes: 1

Views: 23425

Answers (1)

David Zemens
David Zemens

Reputation: 53623

You can maybe use:

IE.Visible = True
Application.SendKeys "~" '## send the "Enter" keystroke

But in my attempts to use SendKeys method, they're not very reliable, and most people will suggest using different methods.

I think personally it is preferable to create another object variable and invoke the button's .Click method, as you indicated.

Upvotes: 1

Related Questions