'Sendkey ("{ENTER}") not working when automating Internet explorer
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
Solution 1:[1]
Solution 2:[2]
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
Solution 3:[3]
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(){}});"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Adassko |
