0

I'm trying to use VBA to access a website and download some information. In the website are some drop-down boxes that need to be selected before the submit button can be clicked. I figured out one of the boxes, but the other one, which is a date, has no options in the html, even though the drop-down is populated by YYYY MMM going back for several years. I found a section of html that seems to be a function which populates the drop-down. I'm certainly not an expert at html so this is my best guess.

The following is the html for the dropdown:

<td valign="top">
  <select name="dateSelect" id="dateSelect" ></select>
</td>

And the function which populates the drop-down looks like:

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);
   }

In my macro, I want to select last month from the dropdown. As a noob, the only thing I could think of to try was:

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")

When I try that, it gives me a Runtime Error '438': Object doesn't support this property or method.

1
  • You're missing semicolons on some of your lines. They aren't strictly required, but they can help avoid errors down the road.
    – Dai
    Commented Apr 1, 2014 at 23:33

1 Answer 1

0

you need to supply a bit more of your code other than the one line, what is IE, is it a IE document, the browser? Going to guess a few parts but the answer is below

'ie - assuming this is the browser
Dim hElem
hElem = ie.Document.getElementsByName("dateselect")
hElem.selectedIndex = hElem.Length - 1
1
  • Yes ie is the browser. I edited my question to show more code. The code that finally worked is: Set hElem = ie.document.getelementbyid("dateselect")
    – iboblaw
    Commented Apr 2, 2014 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.