繁体   English   中英

VBA 自动化(宏)不适用于 IE 选项卡?

[英]VBA Automation (macro) isn't working on IE tabs?

我是新来的,我的英语不太好,这是我的第一个问题,我是 VBA 新手,所以,请放轻松 =D

我试图运行一个宏来自动化 IE 上的一些程序。 我的宏在windows 上打开时效果很好,但是当我尝试在选项卡上运行相同的宏时,它不起作用。 似乎(也许)宏无法识别实际的选项卡。 你能帮我吗,伙计们? 我有点绝望,我的老板几乎要杀了我 o 提供这个解决方案。 谢谢!

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*  

我试图在我这边测试你的代码,发现在你打开第二个选项卡后,你的 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

输出:

在此处输入图片说明

此外,您可以尝试根据您的要求修改代码示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM