簡體   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