Reputation: 328
I am dealing with a web form text field that will initiate a search on its contents, when "Enter" is pressed.
I know how to initiate all of the other event listeners but I am unable to get the press "Enter" event to trigger. It is not listed with the other events. i.e. onchange, onclick, onblur
I am using the CreateObject("Shell.Application") as the parent object in Excel VBA to control an existing IE browser.
I tried sendkeys but have trouble with what VBA focuses on. It types in my IDE or on the spreadsheet itself.
It is not a public site. It is an input tag with Events (blur, change, focus, keydown, mousedown, mousemove).
With Tex_Box
.focus
.keydown
.innertext = Field_Text
.change
.focus
.blur
End With
Upvotes: 1
Views: 5100
Reputation: 328
So I found that stackoverflow.com/questions/11655591/… describes how to provide Internet Explorer with focus before Sendkeys are used well. However, "how" was merely part of the puzzle, the other part was finding "what" needed focus before the program would invoke the sendkey "~"(Enter). Due to an unknown number of sendkey "{TAB}"'s that would have taken to land the cursor on the desired field, I created a loop that would continuously iterate through all the text fields until the partial-string "focus" was present in the attribute titled classname of the desired text box. Once that occurs, the program would have a marker to let it know to invoke the sendkey "~."
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
Dim HWNDSrc As Long
text_again:
With DOCK_Tex_Box
.focus
.keydown
.innertext = Field_Text
.change
Sleep (500)
.focus
.blur
HWNDSrc = IE.HWND
For i = 1 To 100
DoEvents
SetForegroundWindow HWNDSrc
DoEvents
Application.SendKeys ("{TAB}"), True
DoEvents
Sleep 1000
If InStr(.classname, "prompt") > 0 Then 'If the for loop goes to quickly the field loses focus and then the text clears out.
DoEvents
Sleep 500
GoTo text_again:
End If
If InStr(.classname, "focus") > 0 Then 'Text field has focus
Sleep 1000
Exit For
End If
Next
End With
Application.SendKeys ("~"), True
DoEvents
Sleep 500
Upvotes: 1