【问题标题】:Run my VBA code from a existing (opened) internet explorer page从现有(打开的)Internet Explorer 页面运行我的 VBA 代码
【发布时间】:2020-09-26 06:29:25
【问题描述】:

我想在我已经打开并登录的特定 Internet Explorer 页面上运行我的 VBA 宏。

这是因为我必须先登录我的帐户并绕过验证码。 在这一年中,我必须向数百人发送这条极其重复的信息。

以下问题是我打开了一个全新的页面,我将无法绕过验证码。

Dim IE As InternetExplorer

  Set IE = CreateObject("InternetExplorer.Application")

  IE.navigate ("website")

  IE.Visible = True

End Sub

解决方案

Sub TestGetIE()

    Dim IE As Object
    
    'GetIE runs the Functoin we have created below
    Set IE = GetIE("website opened in IE here ")
    WaitFor IE

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
    'Loop through all the opened internet explorer pages
        sURL = ""
        'Loops through all the pages opened on internet explorer
        'Then we will tell our macro to work on that page
        sURL = o.LocationURL
        If sURL Like sLocation & "*" Then
            Set retVal = o
            Exit For
        End If
    Next o

    Set GetIE = retVal
    

End Function

【问题讨论】:

标签: vba internet-explorer


【解决方案1】:

根据您的描述,我了解到您希望用户交互来填充验证码,然后希望继续自动化同一页面。

我建议您在这两个步骤中尝试使用 2 个不同的模块并在两个模块中使用相同的 IE 对象。

您可以创建一个全局 IE 对象,您可以在两个模块中使用它。

然后登录部分执行第一个模块。

之后,用户将手动填写验证码。

然后执行第二个模块来自动化页面的其余部分。

示例代码:

Public ie As Object

Sub login()

    Set ie = CreateObject("internetexplorer.application")
    ie.Visible = True
    ie.navigate "Your_web_site_URL_here..."
    
     Do While ie.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
    
    ie.document.getElementById("uname").Value = "user1"
    ie.document.getElementById("pswd").Value = "12345"
    
End Sub

Sub second_part()
   ie.document.getElementById("email").Value = "user1@abc.com"
   ie.document.getElementById("age").Value = "30"
End Sub

UserForm1 代码:

Private Sub CommandButton1_Click()
Call Module1.login
End Sub

Private Sub CommandButton2_Click()
Call Module1.second_part
End Sub

输出:

【讨论】:

    【解决方案2】:

    调用这个函数帮我解决了。

    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
        'Loop through all the opened internet explorer pages
            sURL = ""
            'Loops through all the pages opened on internet explorer
            'Then we will tell our macro to work on that page
            sURL = o.LocationURL
            If sURL Like sLocation & "*" Then
                Set retVal = o
                Exit For
            End If
        Next o
    
        Set GetIE = retVal
        
    
    End Function
    

    【讨论】:

    • 感谢您发布此问题的解决方案。我建议你尝试在 48 小时后标记你自己对这个问题的答案,当它可以标记时。它可以在未来帮助其他社区成员解决类似的问题。感谢您的理解。
    【解决方案3】:

    您可以做的一件事是 DoEvents,直到页面上出现某个元素。运行您的代码以导航到网站,然后在验证码出现后执行直到元素 DoEvents。代码将循环,直到您登录网站。我喜欢在 GetHub 上使用 VBA SeleniumBasic ChromeDriver。它使网页抓取更容易,您无需使用 Internet Explorer。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 2017-04-08
      • 2016-03-13
      • 1970-01-01
      • 2019-05-14
      相关资源
      最近更新 更多