I am trying to create a macro which pulls a report from a website and this has four drop-down lists to select values. I am able to login to the page and direct myself to the report page, but for some reason am having trouble with the drop-down lists. I have tried several 'solutions' which were provided online, but I keep getting an error message:
Run-time error '438': Object doesn't support this property or method.
Here is one of the drop-downs to select from:
<select name="LocationID">
<option value="0" selected="">All Location</option>
<option value="9">Atlanta</option>
<option value="7">Denver</option>
<option value="3">Las Vegas</option>
<option value="1">Los Angeles</option>
<option value="4">Miami</option>
<option value="6">New Jersey</option>
<option value="10">Phoenix</option>
<option value="2">San Francisco</option>
<option value="8">Seattle</option>
<option value="11">Vancouver</option>
</select>
And here is what I currently have in VBA:
Option Explicit
Const MyUserID As String = "test123"
Const MyPassword As String = "test123"
Const READYSTATE_COMPLETE As Integer = 4
Dim objIE As Object
Public Sub LoginScript()
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
.Visible = True
.Silent = True
.navigate ("https://wwww.mywebsite.com")
Do Until .readyState = READYSTATE_COMPLETE
DoEvents
Loop
Application.Wait Now() + TimeValue("00:00:02")
.document.all.txtuserid.Value = MyUserID
.document.all.txtPassword.Value = MyPassword
objIE.document.getElementsByName("btnSubmit")(0).Click
Do Until .readyState = READYSTATE_COMPLETE
DoEvents
Loop
Application.Wait Now() + TimeValue("00:00:02")
.navigate("https://www.mywebsite.com/sample.html")
Do Until .readyState = READYSTATE_COMPLETE
DoEvents
Loop
.document.getElementByName("LocationID").Value = "7"
objIE.document.getElementsByName("view")(0).Click
End With
End Sub
I assume you are getting your error message on this line?..
.document.getElementByName("LocationID").Value = "7"
That would be because the Name
is an element collection. Unlike ID (getElementByID
), element is plural on collections.
The proper syntax in this case would be:
.document.getElementsByName("LocationID")(0).Value = "7"
Notice the appended (0)
as well? This is because again it's a collection, so you will also need to select the collection item as well. (may not necessarily be (0)
, but I cannot tell you for sure without looking at the entire HTML code.)