我尝试使用VBA访问网站并下载一些信息。在网站上有一些下拉框需要在点击提交按钮之前进行选择。我找到了其中一个盒子,但是另一个是日期,在html中没有选项,即使YYYY MMM的下拉量已经存在了几年。我发现了一段html似乎是一个填充下拉列表的函数。我当然不是html的专家,所以这是我最好的猜测。
以下是下拉列表的html:
<td valign="top">
<select name="dateSelect" id="dateSelect" ></select>
</td>
填充下拉列表的功能如下:
var dateSelect = document.getElementById("dateSelect");
//sel value = YYYYMMM
var dateYear = dateSelect.value.substr(0, 4)
var dateMonth = dateSelect.value.substr(5)
var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var years = new Array()
var d = new Date();
var startYear = 0;
if (d.getYear() < 2000)
startYear = d.getYear() + 1900;
else
startYear = d.getYear();
for (var a = startYear; a >= 2007; a--) {
years[startYear - a] = a;
}
function fillselboxes(theDate) {
var d = new Date();
theDate = new Date(theDate);
var dateSelect = document.getElementById("dateSelect");
for (var a = 0; a < years.length; a++) {
for (var b = months.length - 1; b >= 0; b--) {
addOption(dateSelect, years[a] + " " + months[b], years[a] + "_" + (b + 1));
}
}
dateSelect.value = years[startYear - theDate.getFullYear()] + "_" + (theDate.getMonth() + 1);
}
在我的宏中,我想从下拉列表中选择上个月。作为一个菜鸟,我唯一能想到的就是:
Dim ie as InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "redacted"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
ie.getElementById("dateselect").Value = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "YYYY MMM")
当我尝试时,它会给我一个运行时错误&#39;&#39;:对象不支持此属性或方法。
答案 0 :(得分:0)
你需要提供更多的代码而不是一行,什么是IE,它是IE文档,浏览器?要猜几个部分但答案在下面
'ie - assuming this is the browser
Dim hElem
hElem = ie.Document.getElementsByName("dateselect")
hElem.selectedIndex = hElem.Length - 1