Reputation: 70
I've got a code which opens a webpage and clicks on a link. This then loads data from an Iframe, which I'm trying to change values on dropdown lists.
I'm trying to populate the Manufr
dropdown list from the Year
iFrame. using VBA, but I'm getting a Object Variable or With Block Variable not set
error. Could this have something to do with the fact that the data I am trying to edit is in an Iframe?
Here is the code I'm trying to use
Sub Scrape2()
Dim Browser As InternetExplorer
Dim Doc As HTMLDocument
Dim element As IHTMLElement
Set Browser = New InternetExplorer
Browser.Visible = True
Browser.navigate "http://catalog.xxxxxxx.com"
Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
DoEvents
Loop
For Each l In Browser.document.getElementsByTagName("a")
If l = "http://catalog.xxxxx.com/Catalog.asp?VehicleRef=2" Then
l.Click
Exit For
End If
Next
Stop
Set Doc = Browser.document
Dim mainIframe
Dim subIframe
Set mainIframe = Doc.frames.Item(1).document
Set subIframe = mainIframe.frames.Item(0).document
Set element = mainIframe.getElementById("Manufacturer").selectedIndex = 1
element.FireEvent ("onchange")
Set Doc = Nothing
Set Browser = Nothing
End Sub
Upvotes: 1
Views: 568
Reputation: 166540
Try:
Dim d2
Set d2 = Doc.frames("Year").document
Set element = d2.getElementById("Manufacturer").selectedIndex = 1
element.FireEvent ("onchange")
EDIT: try this
Dim d2, evt
Set d2 = Doc.frames("main").document.frames("year").document
Set element = d2.getElementById("Manufacturer")
If Not element Is nothing Then
Debug.Print "Got drop-down"
element.selectedIndex = 1
'either this...
element.FireEvent ("onchange")
'or try this...
Set evt = d2.createEvent("HTMLEvents")
evt.initEvent "change", True, False
element.dispatchEvent evt
Else
Debug.Print "couldn't get drop-down!"
End if
Upvotes: 1