我正在用 VBA 编写代码以在一些网站中输入在搜索字段中输入日期,然后获取为该日期找到的数字操作系统列表。
当我按 F8 键调试时,代码可以工作,但是当我运行宏时,它有时会工作,有时却不会。当我收到错误消息时,我只需按调试,然后按 F5 继续宏,它应该可以正常工作。问题总是发生在以下方面:
Call IE.document.GetElementsByID("........")
错误消息是:运行时错误“424”,该对象是必需的。
我认为问题在于页面未加载,但我不确定。
Sub PegarDadosListas(data As Date)
Dim contador As Integer
Dim dia As String
Dim mes As String
Dim ano As String
dia = Day(data)
mes = Month(data)
ano = Year(data)
Range("K2").End(xlToRight).Offset(0, 1) = data
Call Extra(dia, mes, ano)
Call Pontofrio(dia, mes, ano)
End Sub
Sub Extra(dia As String, mes As String, ano As String)
Dim URL As String
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
URL = "http://www.extra.com.br/listadecasamento/home.aspx"
IE.Navigate URL
Do While IE.Busy
DoEvents
Loop
Call IE.document.getelementbyid("ctl00_Conteudo_PaginaSistemaArea1_ctl04_txtDia").setattribute("value", dia)
Call IE.document.getelementbyid("ctl00_Conteudo_PaginaSistemaArea1_ctl04_txtMes").setattribute("value", mes)
Call IE.document.getelementbyid("ctl00_Conteudo_PaginaSistemaArea1_ctl04_txtAno").setattribute("value", ano)
IE.document.getelementbyid("ctl00_Conteudo_PaginaSistemaArea1_ctl04_btnEncontrarLista").Click
Do While IE.Busy
DoEvents
Loop
Sheets("Plan2").Range("A4") = IE.document.getelementsbyclassname("pagination")(0).innertext
Sheets("Plan2").Range("A2").FormulaR1C1 = "=MID(R4C1,R3C1,40)"
Sheets("Plan2").Range("A3").FormulaR1C1 = "=FIND(""pesquisa"",R4C1)"
IE.Quit
Call CopiaeCola(3)
End Sub
Sub Pontofrio(dia As String, mes As String, ano As String)
Dim URL As String
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
URL = "http://www.pontofrio.com.br/Site/ListaGerenciadaCasamentoWelCome.aspx"
IE.Navigate URL
Do While IE.Busy
DoEvents
Loop
With IE
Call .document.getelementbyid("ctl00_Conteudo_ctl01_CtrlBuscarLista_txtDia").setattribute("value", dia)
Call .document.getelementbyid("ctl00_Conteudo_ctl01_CtrlBuscarLista_txtMes").setattribute("value", mes)
Call .document.getelementbyid("ctl00_Conteudo_ctl01_CtrlBuscarLista_txtAno").setattribute("value", ano)
.document.getelementbyid("ctl00_Conteudo_ctl01_CtrlBuscarLista_btnEncontrarLista").Click
Do While IE.Busy
DoEvents
Loop
Sheets("Plan2").Range("A4") = IE.document.getelementsbyclassname("pagination")(0).innertext
Sheets("Plan2").Range("A2").FormulaR1C1 = "=MID(R4C1,R3C1,40)"
Sheets("Plan2").Range("A3").FormulaR1C1 = "=FIND(""pesquisa"",R4C1)"
End With
IE.Quit
Call CopiaeCola(4)
End Sub