2016-03-08 102 views
0

我正在尝试设置下拉框的值,并显示一个甚至不是选项的空白。VBA无法通过选择下拉菜单获得选项

下面是HTML剪断,它(注:内部网站,所以我不能给URL或太多)

<select id="entNo" name="entNo" style="width:75px"> 
    <option selected="selected" value="All">All</option> 
    <option value="650101">650101</option> 
    <option value="66">66</option> 

这里是它的功能。

function requestEntities() 
    { 
     var ele = document.getElementById('entNo'); 
     if (ele) { 
      ele.options.length = 0; 
      ele.options[0] = new Option('All'); 
      ele.disabled = true; 
      ele = document.getElementById('divNo'); 
      if (ele) { 
       var i = ele.selectedIndex; 
       var div = ele.options[i].value; 
       sendHttpRequest('http://example.com/cgi-bin/wspd_cgi.sh/WService=wslive//wpr/entityList.r?div=' + div); 
      } 
     } 
    } 

我使用VBA代码是这样的:

Set objCollection = ie.document.getElementsByTagName("select") 
    'Debug.Print objCollection.Length 

    i = 1 
    While i < objCollection.Length  'Loop to locate and change the fields with proper data using the "select" tags 
     Debug.Print objCollection(i).Value 
     If objCollection(i).Name = "entNo" Then 
      If ecode <> "" Then 
      Debug.Print objCollection(i).Value 
       objCollection(i).Value = ecode 
      End If 

     ElseIf objCollection(i).Name = "curStatus" Then 
      objCollection(i).Value = "Open" 

     ElseIf objCollection(i).Name = "opStatus" Then 
      objCollection(i).Value = "All" 


     End If 
      i = i + 1 
    Wend 

我不能给更多的,因为这是工作。我已经耗尽谷歌搜索。
另一件奇怪的事情是,当我单步执行时它会起作用。否则就无法工作。 任何建议将不胜感激。

+0

我无法获得当前选定的值。 – user3490906

+0

如果这些select元素在某种程度上是依赖的(改变其中一个会改变下一个可用的选项),那么你需要等待下一个填充,这就是为什么它在单步执行时工作,但不是如果你直接运行整个事情。 –

回答

1

忽略时间问题,现在,你的代码会更容易为:似乎

If ecode <> "" Then ie.document.getElementById("entNo").Value = ecode 
ie.document.getElementById("currStatus").Value = "Open" 
ie.document.getElementById("opStatus").Value = "All" 

由于您选择的元素有ID属性。

+0

太棒了。谢谢回答。这比我做的要干净得多。 – user3490906

+0

我发现我的系统有些奇怪的事情发生。我重新启动,问题消失了。奇怪的是,但我关闭所有的应用程序,并再次尝试,但最终归结为重新启动。下次我会在发布之前重新启动系统。 – user3490906