0

The following excel vba macro will open a webpage and insert "500010" into a text box. If you manually type the same number into the textbox, a dropdown box will appear with further selections. This doesn't occur with the programmatic number entry. I've tried a number of ways to programmatically get this dropdown box to appear, a few are shown in my code, but to no avail.

Sub test()

URL = "http://www.bseindia.com/markets/equity/EQReports/StockPrcHistori.aspx?expandable=7&flag=0"

Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate URL

Do Until (ie.readyState = 4 And Not ie.Busy)
    DoEvents
Loop

ie.Document.getElementById("ctl00_ContentPlaceHolder1_GetQuote1_txtscrip_code").Value = 500010 

' click the textbox to get the dropdown box to appear
ie.Document.getElementById("ctl00_ContentPlaceHolder1_GetQuote1_txtscrip_code").fireevent ("onClick")
ie.Document.getElementById("ctl00_ContentPlaceHolder1_GetQuote1_txtscrip_code").Click
end sub

If I look at the source code for the webpage, I see the following prior to information about the textbox.

<script type="text/javascript">
  function cleartext1(a) {
          if (a.value == "Scrip code/Scrip Name")
              a.value = '';

      }
      function Filltext1() {
          a.value == "Scrip code/Scrip Name"

      }
     function ClearTextBox(a)
    {    
        if (a.value == a.defaultValue) a.value = "";
    }
    function FillTextBox(a) 
    {
        if (a.value == "") a.value = a.defaultValue;
    }
    function HiddenValue(hdn) { 

        var hvalue = document.getElementById(hdn).value;
        if (hvalue != "") 
        {  
            var s1=hvalue.split("|");
            location.href = "/StockReach/AdvanceStockReach.aspx?scripcode=" + s1[0];
            return true;
        }
       else
            return false;
    }

//    function chk(e)
//    {
//    if(window.event)
//    {
//        var key=window.event.keyCode;
//        if (key == 13) 
//        {
//            var btn = document.getElementById('btnGetQuote');
//            //  HiddenValue('hdnIdAsset');
//            
//          btn.click();
//          //btn.focus();
//        }
//      }
//    }
</script>
<div id="ctl00_ContentPlaceHolder1_GetQuote1_Pn1" onkeypress="return noenter(event);">

<table border="0" cellspacing="0" cellpadding="2" align="left">
    <tr>
        <td style ="padding-right:10px;">
            <input value="" id="divshow" type="hidden" />
            <input value="" id="hdnIdAsset" type="hidden" /><input name="ctl00$ContentPlaceHolder1$GetQuote1$hdnValue" type="hidden" id="ctl00_ContentPlaceHolder1_GetQuote1_hdnValue" />

            <input name="ctl00$ContentPlaceHolder1$GetQuote1$txtscrip_code" type="text" maxlength="500" id="ctl00_ContentPlaceHolder1_GetQuote1_txtscrip_code" value="Scrip code/Scrip Name" class="textbox2" onclick="javascript:selecttxt(this);" onkeypress="javascript:noenter(event);return chkkey(event,this);" onfocus="cleartext1(this);" onblur="FillTextBox(this)" onkeyup="javascript:showDivSelect(event,'Asset','http://www.bseindia.com/common/backpageAsset.aspx',this,this.value,3,'0',false);" style="width:160px;" />

I'm not very familiar with writing javascript functions for vba, but I tried putting the following in my macro, but it created an error.

f = "function (hiddenvalue) 'http://www.bseindia.com/markets/equity/EQReports/StockPrcHistori.aspx?/StockReach/AdvanceStockReach.aspx?scripcode=500010').value = 'True';}" ie.Document.parentWindow.execScript f, "jscript" ie.Document.getElementById("ctl00_ContentPlaceHolder1_GetQuote1_txtscrip_code").Click

Am I on the right track, is a javascript function in my code what's needed here? Can someone help me come up with the code to get the dropdown box to appear?..TIA, Ron

ron
  • 1,456
  • 3
  • 18
  • 27
  • 1
    What you're looking for is similar to the behavior of a combo box, which doesn't exist in webpages. These behaviors are "faked" through custom code or a library. Usually what you'll see as the dropdown is a div containing spans, or a ul containing li's. Look into JQuery-UI autocomplete for a somewhat easy solution. – MaKR Jan 24 '14 at 19:42
  • Thanks, but when I searched "JQuery-UI autocomplete, vba", there were no hits – ron Jan 24 '14 at 19:51

1 Answers1

0

The input box where you write the value 500010 calls a javascript function 'showDivSelect' in event 'onkyeup'. Inside of this function ajax call is made to the page http://www.bseindia.com/common/backpageAsset.aspx and the results are shown in the div 'divSelAsset_1' (has display:none). So you have to fire 'onkeyup' event from VBA but have to pass the corrent event argument (which is prpbably the IDOMKeyboardEvent instance ... is it possible to create KeyboardEvent object in VBA?).

Dim evt As IDOMKeyboardEvent
Set evt = doc.createEvent("KeyboardEvent")
' evt.Key = "5" ... Key is read-only, so how to say which Key should the event be for?

Dim txtscrip_code As HTMLInputElement
Set txtscrip_code = ie.document.getElementById("ctl00_ContentPlaceHolder1_GetQuote1_txtscrip_code")

txtscrip_code.Focus
txtscrip_code.FireEvent "onkeyup()", evt

... But this will not work :-(.

Maybe you could try to call the function 'showDivSelect' from VBA directly. This function has this signature:

function showDivSelect(e, flag, page, objtxtinput, valobjtxtinput, mintxtlength, what, varbool, bSusp)

Execute javascript from VBA could be possible like this:

ie.document.parentWindow.execScript("code", "JavaScript") 

Have a look e.g. here: http://msdn.microsoft.com/en-us/library/ie/ms536420(v=vs.85).aspx

In javascript there you can create keyboard event like this: In JavaScript, how can I create my own Keyboard event?

Good luck!

Community
  • 1
  • 1
Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51