我是新来的,我的英语说得不好,这是我的第一个问题,我是VBA的新来的,所以,请对我好一点= D
我正在尝试运行宏以自动化IE上的某些过程。在 windows 上打开时,我的宏工作得很好,但是当我尝试在选项卡上运行相同的宏时,它不起作用。似乎(也许)宏无法识别实际的标签。伙计们,您能帮我吗?我有点绝望,我的老板差点要杀了我,要提供这个解决方案。谢谢!
Dim ie As Object
Dim dic As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
On Error Resume Next
ie.navigate "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/ConsultaPublicaAreasEmbargadas.php"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = ie.document
'IE.document.getElementById("num_cpf_cnpj").Value = "1234"
On Error Resume Next
doc.getElementById("num_cpf_cnpj").Value = Sheets("Main").Range("C10")
doc.getElementById("Emitir_Certificado").Click
*until here works fine*
ie.Visible = True
ie.navigate "http://www.cnj.jus.br/improbidade_adm/consultar_requerido.php?validar=form", CLng(2048)
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = ie.document
document.getElementById("num_cpf_cnpj").Value = Sheets("Main").Range("C10") --->>>>>> *but this line doesn't work, the tab opens but nothing happens*
答案 0 :(得分:0)
我试图从一边测试您的代码,发现打开第二个选项卡后,您的IE对象仍引用第一个选项卡并将值插入其中。
我尝试修改您的代码,并在其中添加了一些代码以查找并移至第二个选项卡,并将值插入该选项卡。
Sub demo()
Dim i As Long
Dim URL As String
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/ConsultaPublicaAreasEmbargadas.php"
IE.Navigate2 URL
Do While IE.readyState = 4: DoEvents: Loop 'Do While
Do Until IE.readyState = 4: DoEvents: Loop 'Do Until
IE.document.getElementById("num_cpf_cnpj").Value = "demo" 'Sheets("Main").Range("C10")
IE.document.getElementById("Emitir_Certificado").Click
IE.Navigate2 "http://www.cnj.jus.br/improbidade_adm/consultar_requerido.php?validar=form", 2048&
Application.Wait (Now + TimeValue("0:00:05"))
Set IE = GetIE("http://www.cnj.jus.br/improbidade_adm/consultar_requerido.php?validar=form")
IE.document.getElementById("num_cpf_cnpj").Value = "demo1"
'Unload IE
' Set IE = Nothing
' Set objElement = Nothing
' Set objCollection = Nothing
End Sub
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next 'because may not have a "document" property
'Check the URL and if it's the one you want then
' assign the window object to the return value and exit the loop
sURL = o.document.Location
On Error GoTo 0
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function
输出:
此外,您可以尝试根据需要修改代码示例。