Text entered into Webpage Search Box via Excel VBA is not detected when clicking Search button

你离开我真会死。 提交于 2019-12-01 01:24:03

After hours of searching, scouring over each section of code, and much digging around countless forums I finally found a solution that may help others in the future. I realized that my issue has to deal with the way the value/textbox updates (using the valueUpdate: 'afterkeydown', which means the code is looking for actual keypress events, which is why it wouldn't see the string when I inserted it into the text field via the vba code. Since I discovered that .fireEvent() doesn't work on newer versions of Internet Explorer, I felt like all was lost and that I would have to revert to the unreliable Sendkeys() method that everyone says to avoid. But, just before I was about to give up for the night I came across this little gem where user dyanisis2 provides this method after their long search for the same issue yielded this solution

Set evt = ie.Document.createEvent("keyboardevent")
    evt.initEvent "change", True, False
    PW.all(0).dispatchEvent evt

Replace PW with your object.

Placing this bit of code after entering a value into the field that needs to detect a keypress will simulate a keypress event, therefore updating the value in the text field and allowing the code to recognize that there is something in the field.

Charles

Just want to confirm this worked for me. just replaced PW.all.(0) at the last line.

IE.Document.forms(0).getElementsByTagName("select")(1).dispatchEvent evt
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!