1

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
Community
  • 1
  • 1
Josh Anstead
  • 328
  • 2
  • 15
  • 1
    It is an interesting question, but some more code (preferably enough to make it a [mcve]) would help. – John Coleman Aug 03 '17 at 00:59
  • Sorry, I do not know of any other site, that is public, with an example. – Josh Anstead Aug 03 '17 at 01:00
  • Looking now. Seems that "Press Enter to Search" is a button-less method of initiating an event. – Josh Anstead Aug 03 '17 at 01:04
  • If it's an actual text input then you should be setting its Value , not the innerText. Try appending a new line/carriage return on the end of the value (vbLf, vbCR or maybe vbCrLf) – Tim Williams Aug 03 '17 at 01:14
  • Tried that on value and it did not work. Thanks for the idea. – Josh Anstead Aug 03 '17 at 01:41
  • 2
    here is a link that may help https://stackoverflow.com/questions/11655591/ie-9-not-accepting-sendkeys – jsotola Aug 03 '17 at 02:04
  • i saw a code example somewhere ... i don't remember where ... a year number had to be entered in a webpage dropdown. so part of the code was `... control.Value = "201"` and the second part was `SendKeys "7"` ... that would select "2017" and produce the keypress needed by the webpage in order to accept the entry – jsotola Aug 03 '17 at 02:52
  • Thanks everyone for all the help. It was @jsotola"s first comment that lead me to the document I needed. I had seen it before but the difference this time was I could see the website flicker when the sendkey "~" was working. I also could see the classname change when the text field had focus. So I created a for loop with Sendkeys "TAB" until classname = focus then Sendkey "~". – Josh Anstead Aug 03 '17 at 04:19

1 Answers1

1

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
Josh Anstead
  • 328
  • 2
  • 15