VBA Automation(宏)在IE选项卡上无法很好地工作

时间:2019-12-10 18:13:10

标签: excel vba internet-explorer automation

我是新来的,在VBA上,这是我的第二篇文章,我的英语说得不太好。所以,请对我轻松= D

上周,我在尝试自动执行IE上的某些程序时遇到了一些麻烦。在新窗口上打开时,我的宏工作得很好,但是当我尝试在选项卡上运行相同的宏时,它却无法正常工作。似乎(也许)宏无法识别实际的标签。我在这里发布了这个问题,一个名叫Deepak-MSFT的绝妙男人对我有很大帮助。他教我如何在标签上运行宏,该宏几乎可以100%工作。

问题:

1-宏有时有效,有时无效。例如,昨天,我遇到错误424、91,有时,它只是打开了另一个选项卡而没有插入该值(由于“ On Error Resume Next”,所以会出现提示)。我认为这是因为其中一个网站运行缓慢。我试图将TimeValue增加到7秒,但即使这样,它也没有起作用。我整天都进行了测试,它非常不稳定,有时可以工作,有时却不能。但是今天它正在运行(所有网站都稳定)。知道如何解决这种不稳定性吗?

2-我有2个网站(第2个和第3个),其中我使用querySelector查找用于插入值的框。它们都可以在我公司的70%的PC上运行,而只有这两个网站在其他30%的PC上无法运行。我在所有它们上安装了相同版本的IE(11.0.9600),Excel(2007,SP3)和Windows(W7),但是即使如此,只有那些带有queryselector的两个网站无法正常工作。没有错误。宏只是不插入值。也许我缺少使queryselector在那些PC上运行的功能,但是所有引用都匹配。我在Google上挖了前5页,试图找到解决方案,但失败了。我认为这些PC周围有一个女巫。你能帮我吗?

3-我在尝试在Windows 10上的Excel 2016或高于2007的其他版本的Excel上运行宏时完全不稳定,是否有人有很好的解决方案来使程序适应在Excel上运行2016年(我公司所有的公司都将在明年运行此版本)而不必再次做遍吗?

非常感谢你们。这是我的代码:

Sub ademo()

    Dim i As Long
    Dim URL As String
    Dim IE As Object

    Set IE = CreateObject("InternetExplorer.Application")


    IE.Visible = True

    'TAB 1

    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 = "demo1" 'Sheets("Main").Range("C10")
    IE.document.getElementById("Emitir_Certificado").Click



 'TAB 2 - DAP

    On Error Resume Next
    IE.Visible = True
    IE.Navigate2 "http://smap14.mda.gov.br/extratodap/", 2048&


    Application.Wait (Now + TimeValue("0:00:04"))

    Set IE = GetIE("http://smap14.mda.gov.br/extratodap/")

    Do While IE.Busy
            Application.Wait DateAdd("s", 1, Now)
        Loop

        Set doc = IE.document

        On Error Resume Next
        Set Target = IE.document.querySelector("#corpo > div > div > div:nth-child(1) > button")
        Target.Click
            Do While IE.Busy
            Application.Wait DateAdd("s", 1, Now)
            Loop
            doc.getElementById("txtCPF_CNPJ").Value = "demo2"




    'TAB 3 - CNDT

    IE.Visible = True
    On Error Resume Next
    IE.Navigate2 "http://aplicacao.jt.jus.br/cndtCertidao/inicio.faces", 2048&


    Application.Wait (Now + TimeValue("0:00:04"))

    Set IE = GetIE("http://aplicacao.jt.jus.br/cndtCertidao/inicio.faces")

    Do While IE.Busy
            Application.Wait DateAdd("s", 1, Now)
        Loop

        Set doc = IE.document

        On Error Resume Next
        Set target2 = IE.document.querySelector("#corpo > div > div:nth-child(2) > input:nth-child(1)")
        target2.Click
            Do While IE.Busy
            Application.Wait DateAdd("s", 1, Now)
            Loop
            doc.getElementById("gerarCertidaoForm:cpfCnpj").Value = "demo3"






    'TAB 4 - CND

    On Error Resume Next
    IE.Visible = True
    IE.Navigate2 "http://servicos.receita.fazenda.gov.br/Servicos/certidao/CNDConjuntaInter/InformaNICertidao.asp?tipo=2", 2048&


    Application.Wait (Now + TimeValue("0:00:04"))

    Set IE = GetIE("http://servicos.receita.fazenda.gov.br/Servicos/certidao/CNDConjuntaInter/InformaNICertidao.asp?tipo=2")

    Do While IE.Busy
            Application.Wait DateAdd("s", 1, Now)
        Loop

        Set doc = IE.document

        On Error Resume Next
        IE.document.getElementsByName("NI")(0).Value = "demo4"



    'TAB 5 - IMPROBIDADE

    IE.Visible = True
    IE.Navigate2 "http://www.cnj.jus.br/improbidade_adm/consultar_requerido.php?validar=form", 2048&


    Application.Wait (Now + TimeValue("0:00:04"))

    Set IE = GetIE("http://www.cnj.jus.br/improbidade_adm/consultar_requerido.php?validar=form")

    IE.document.getElementById("num_cpf_cnpj").Value = "demo5"



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

0 个答案:

没有答案