I have a desperate need to program a macro that would help me retrieving "suggested" results from a javascript search box. I have a very long list of names with some unreadable characters (which I have converted to asterisks) and upon submitting these names to such engine I often get suggested the relevant "clean" name.
The webpage has a lot of javascript code so my task ultimately consists in instructing excel to control internet explorer just like I would do it manually. For copyright reasons I cannot post the entire source of this webpage, but I have identified the two main elements I need to control. First, there is an input box where I need to input the search key:
<div class="quickSearchCriteria">
<input name="SearchText" type="text" id="SearchText_08"
class="quickSearchCriteriaInput" maxlength="150" value="Enter a name"
onkeyup="onClickText(true);GrayOkQuick();if(event.keyCode==13)
{doOkOnEnterForMozilla();return true;}" onblur="onClickText(false);GrayOkQuick();"
onmouseup="onClickText(true);" style="color:gray;" />
I found it pretty straightforward to input the search key in this box:
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
.navigate "%%%"
.Visible = 0
Do While .Busy: DoEvents: Loop
Do While .readyState <> 4: DoEvents: Loop
Set htmlDoc = .Document
Set htmlColl = htmlDoc.getElementsByName("SearchText")
For Each htmlInput In htmlColl
htmlInput.Focus
htmlInput.Value = "Tryme"
Next htmlInput
Upon typing manually the name, I am able to trigger the elaboration of the suggestion box, which will appear in the form of a table:
<table class="sugg_m" cellspacing="0" cellpadding="0" style="visibility: visible; left: 23px; top: 92px; width: 342px;">
which I am also perfectly able to import (and possibly parse) using:
Set htmlsugg = htmltabs.getElementsByClassName("sugg_m")
stringresults = Trim(htmlsugg.item(, 1).outerText)
What I am missing is the link between these two. I am not able to trigger the onkeyup event from the input box. Here is what I have already tried, which proved unsuccessful:
Changing the focus (back and forth)
Use FireEvent Method:
htmlInput.FireEvent "onkeyup"
Using
dispatchEvent
instead ofFireEvent
Set evt = htmlDoc.createEvent("HTMLEvents")
evt.initEvent "blur", False, False
htmlInput.dispatchEvent evt
Executing all seemingly relevant scripts:
htmlDoc.parentWindow.execScript "ActiveNGram()", "JavaScript"
htmlDoc.parentWindow.execScript "onClickText(true)", "JavaScript"
htmlDoc.parentWindow.execScript "SearchTextCreateSugg()", "JavaScript"
htmlDoc.parentWindow.execScript "ShowTable()", "JavaScript"
Using
Call
prior to the statements in 4 also does not work
Note that with unsuccessful I mean that the macro runs smoothly, but the target variable stringresults
is returned as empty (and it doesn't if I manually input the search key and then I extract the table with the macro).
The actual function that recreates the table is located inside a javascript module (which I can access and see manually but I am unable to do so automatically:
<script type="text/javascript" src="/version-201452/Common/Javascript/Search/sugg.js"></script>
Besides, I do not think it is necessary to access the module: I do not need to create the table, I just need to make it appear.
Any help will be greatly appreciated as now I am more than lost, and I can provide more information and codes if needed.
Thanks!