PDA

View Full Version : VBA IE automation: error on getting iframe



baralus
08-23-2018, 12:42 AM
I've been having this wierd problem recently. before this code of mine is working properly in pulling out the data that I need in this website. But recently for some reasons, I started to get an "automation error" when i set the my variable in getting the "iframe". What this code of mine do is simply click a label called "More information" for a table to appear then pull-out the data. Hoping someone can help me. I can't say where is the wrong here. On my code or in the website.



Sub GetData()
Dim IE As SHDocVw.InternetExplorer
Dim html As MSHTML.HTMLDocument
Dim htmlelements As MSHTML.IHTMLElementCollection
Dim htmlelement As MSHTML.IHTMLElement
Dim htmlelements2 As MSHTML.IHTMLElementCollection
Dim htmlelement2 As MSHTML.IHTMLElement

Set IE = New InternetExplorer

With IE
.Visible = True
.Navigate "http://www.anterior.banxico.org.mx/portal-mercado-cambiario/foreign-exchange-markets--exc.html"
Do While .ReadyState <> READYSTATE_COMPLETE: Loop
End With

Set html = IE.Document

'this is where i get the error
Set htmlelements = html.getElementsByTagName("iframe")(0).contentDocument.getElementsByTagName("a")


For Each htmlelement In htmlelements
htmlelement.Click
Application.Wait (Now + TimeValue("00:00:02"))
Set htmlelements2 = html.getElementsByTagName("iframe")(0).contentDocument.getElementsByTagName("tr")
For Each htmlelement2 In htmlelements2
If htmlelement2.className = "renglonNon" And htmlelement2.getAttribute("align") = "center" Then
col = 2
If Not Worksheets("sheet1").Cells(6, 2) = Empty Then
lastcol = Worksheets("sheet1").Cells(6, Columns.count).End(xlToLeft).Column
col = lastcol + 1
Worksheets("sheet1").Cells(6, col) = Trim(Application.Clean(htmlelement2.Children(2).textContent))
Exit For
Else
Worksheets("sheet1").Cells(6, col) = Trim(Application.Clean(htmlelement2.Children(2).textContent))
Exit For
End If
End If
Next
Exit For
Next

end sub