Sendkey ("{ENTER}") not working when automating Internet explorer

22.7k views Asked by At

Sendkey ("{ENTER}") not entering data into website from excel

I need to use it since form does not have a button to click

Sub FillInternetForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://reference.medscape.com/drug-interactionchecker"
IE.Visible = True
While IE.Busy
DoEvents
Wend

Set TrackID = IE.Document.getelementbyid("MDICtextbox")
TrackID.Value = "Aspirin"
IE.Visible = True
SendKeys ("{ENTER}") 

End Sub

Note that, if I change enter to other command such as {BS} or Backspace it works but not enter

3

There are 3 answers

4
Uri Goren On

As a rule of thumb, If you are using SendKeys, you are doing something wrong.

SendKeys is an extremely fragile function, and there are usually other "programmatic" methods

My Advice: Use the Page's javascript

I analyzed http://reference.medscape.com/drug-interactionchecker for you,

It turns out that the handleKeyDown function is bound to the onkeydown event,

You can simply simulate the {ENTER} key by calling it directly

handleKeyDown({which:13,preventDefault:function(){}});

(You can verify that it works by typing it in your browser's console)

You could call the function from vba with:

IE.Document.parentwindow.execscript "handleKeyDown({which:13,preventDefault:function(){}});"
0
CmPi On

Did you try ~ (tilde) as this page suggest it :

SendKeys

1
gokanayz On

Sub FillInternetForm()

Dim IE As Object

Set IE = CreateObject("InternetExplorer.Application")

IE.Navigate "http://reference.medscape.com/drug-interactionchecker"

IE.Visible = True

While IE.Busy

DoEvents

Wend

Set TrackID = IE.Document.getelementbyid("MDICtextbox")

TrackID.Value = "aspirin"

IE.Visible = True

'Application.SendKeys ("{ENTER}")

IE.Document.parentwindow.execscript "handleKeyDown({which:13,preventDefault:function(){}});"

End Sub