1

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:

  1. Changing the focus (back and forth)

  2. Use FireEvent Method: htmlInput.FireEvent "onkeyup"

  3. Using dispatchEvent instead of FireEvent

    Set evt = htmlDoc.createEvent("HTMLEvents") evt.initEvent "blur", False, False htmlInput.dispatchEvent evt

  4. 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"

  5. 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!

Dmitry Pavliv
  • 35,333
  • 13
  • 79
  • 80
Simone S
  • 93
  • 1
  • 8
  • Try the approach suggested in the accepted answer here: http://stackoverflow.com/questions/7518664/how-can-i-generate-a-keyup-event-with-a-specific-keycode-in-ie8 It shows how to use `document.createEventObject("KeyboardEvent")` to generate the appropriate event. Note also that since your input has an id using `getElementById` would be a more direct way to get a reference to it – Tim Williams May 07 '14 at 16:41
  • @TimWilliams: Thanks a lot. I have tried using the following code but did not work: `Dim evt As Object`; `Set evt = htmlDoc.CreateEventObject("KeyBoardEvent")`; `evt.KeyCode = 13`; `htmlInput.FireEvent "onkeyup", evt`. Again, the code runs smoothly but no output is produced. – Simone S May 08 '14 at 08:04
  • Hard to suggest anything else without a URL to look at. – Tim Williams May 08 '14 at 14:47

0 Answers0