title: VBS: target fields by div and tabIndex from external .vbs
body: <p>I am struggling to set the values of some very awkward fields on a webpage (one that I do not control), would value a boffin's input, please. I have made a mock up of the webpage, demonstrating the issue. Here goes:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  
  &lt;div id="mydiv1" style="position: none; display: block; z-index: 1; width: 800px;"&gt;
  &lt;p&gt;First div (mydiv1) starts here.&lt;/p&gt;
  &lt;br /&gt;
  &lt;div id="mydiv2" style="position: none; display: block; z-index: 6; width: 800px;"&gt;
  &lt;p&gt;Second div (mydiv2) starts here. The following field in blue is arranged thusly: &lt;i&gt;div&gt;div&gt;table&gt;form&gt;table&lt;/i&gt;; and its tabIndex is 1.&lt;/p&gt;
  
  &lt;table border="0" cellspacing="1" cellpadding="0" width="410"&gt;
  &lt;tbody&gt;
  &lt;tr&gt;
  &lt;td colspan="3" style="width: 410px; background-color: #5698eb;"&gt;
  
  &lt;form id="entryform" name="entryform"&gt;
  
  &lt;table border="0" cellspacing="1" cellpadding="0"&gt;
  &lt;tbody&gt;
  &lt;tr&gt;
  &lt;td width="100" valign="center" class=text1"&gt;
  &lt;br /&gt;&lt;b&gt;Project: &lt;/b&gt;
  &lt;/td&gt;
  &lt;td width="5"&gt;&lt;/td&gt;
  &lt;td valign="centre"&gt;
  &lt;input class="formclass_input1" type="text" tabIndex="1" name="entryform_field"&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;br /&gt;
  &lt;div id="mydiv3" style="position: none; display: block; z-index: 16; width: 850px;"&gt;
  &lt;p&gt;Third div (mydiv3) starts here. The following fields are arranged thusly: &lt;i&gt;div&gt;div&gt;div&gt;form&gt;table&lt;/i&gt; and the tabIndexes go 1,2,3,4.&lt;br /&gt;The field names change randomly from record to record, but their tabIndexes are always the same.&lt;/p&gt;
  
  &lt;form id="myform" name="myform"&gt;
  
  &lt;table border="0" cellspacing="1" cellpadding="0"&gt;
  &lt;tbody&gt;
  &lt;tr&gt;
  &lt;td width="100" valign="center" class=text1"&gt;
  &lt;b&gt;Name: &lt;/b&gt;
  &lt;/td&gt;
  &lt;td width="5"&gt;&lt;/td&gt;
  &lt;td valign="centre"&gt;
  &lt;input class="formclass_input1" type="text" tabIndex="1" name="changeable_field_name1"&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;table border="0" cellspacing="1" cellpadding="0"&gt;
  &lt;tbody&gt;
  &lt;tr&gt;
  &lt;td width="100" valign="center" class=text1"&gt;
  &lt;b&gt;Date: &lt;/b&gt;
  &lt;/td&gt;
  &lt;td width="5"&gt;&lt;/td&gt;
  &lt;td valign="centre"&gt;
  &lt;input class="formclass_input1" type="text" tabIndex="2" name="changeable_field_name2"&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;table border="0" cellspacing="1" cellpadding="0"&gt;
  &lt;tbody&gt;
  &lt;tr&gt;
  &lt;td width="100" valign="center" class=text1"&gt;
  &lt;b&gt;Company: &lt;/b&gt;
  &lt;/td&gt;
  &lt;td width="5"&gt;&lt;/td&gt;
  &lt;td valign="centre"&gt;
  &lt;input class="formclass_input1" type="text" tabIndex="3" name="changeable_field_name3"&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;table border="0" cellspacing="1" cellpadding="0"&gt;
  &lt;tbody&gt;
  &lt;tr&gt;
  &lt;td width="100" valign="center" class=text1"&gt;
  &lt;b&gt;Address: &lt;/b&gt;
  &lt;/td&gt;
  &lt;td width="5"&gt;&lt;/td&gt;
  &lt;td valign="centre"&gt;
  &lt;input class="formclass_input1" type="text" tabIndex="4" name="changeable_field_name4"&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The critical thing here is the field names change, randomly. I have presented my problem previosuly on stack, but I oversimplified the problem! A fellow named Panayot helped and solved it for me perfectly, but when I introduced my revised script to the live site, it failed. Hence making this more accurate mock-up. It is more complicated than I first presented. Using Panayot's code, my script currently looks like this:</p>
  
  <pre><code>surl ="http://[website.com]"
  set ie = nothing
  set shapp=createobject("shell.application")
  on error resume next
  For Each owin In shapp.Windows
       if left(owin.document.location.href,len(surl))=surl then
          if err.number = 0 then
          set ie = owin
            end if
         end if
  err.clear
  Next
  on error goto 0
  if ie is nothing then
      wscript.echo "Window Not Open"
  else
  
  Set myDiv = IE.Document.All.mydiv3
  
  If Not myDiv Is Nothing Then
      Set nodes = myDiv.childNodes
      For i = 0 To nodes.Length-1 Step 2
          If nodes(i).tabIndex = "1" Then
              nodes(i).Value = "ta-da!"
              'nodes(i).Value = nodes(i).tabIndex
              Exit For
          End If
      Next
  End If
  
  End If
  </code></pre>
  
  <p>I've experimented a lot with it, but I just can't figure it out with my limited understanding. Basically the final IF statement is not getting me into the tabIndex "1". If this is solvable I'll be a very happy guy. Any input is appreciated. Thank you.</p>