【问题标题】:Navigating through websites浏览网站
【发布时间】:2017-11-09 02:38:10
【问题描述】:

我的代码能够与它打开的第一个网页进行交互,但是一旦我离开它,我的代码就会给我一个“424”对象所需的错误。这是一个例子。

EX:访问 yahoo.com - 搜索“地球” - 点击 Yahoo 徽标返回 yahoo 主页

Sub InternetPractice()

Dim ie As InternetExplorer

Set ie = New InternetExplorer

ie.Visible = True

ie.navigate "https://www.yahoo.com"

Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

ie.document.getElementById("uh-search-box").Value = "Earth"
ie.document.getElementById("uh-search-button").Click


Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

ie.document.getElementById("logo").Click

End Sub

在 Internet 上使用 VBA 对我来说是新事物,感谢任何帮助!

【问题讨论】:

    标签: excel internet-explorer vba


    【解决方案1】:

    编辑:

    我认为你没有给 ie 加载足够的时间......它对我有用。 所以你可以试试这个给更多时间的代码,并在中止之前尝试加载 3 次。

    Option Explicit
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Sub InternetPractice2()
    
    
        Dim IE As InternetExplorer
        Dim HTMLDoc As HTMLDocument
        Dim logo As Object
        Dim n As Long
        Set IE = New InternetExplorer                'Set IE = CreateObject("InternetExplorer.Application")
    
        With IE
            .Silent = True
            .Visible = True
            .Navigate "https://www.yahoo.com"
        End With
    
        WaitIE IE, 5000
    
        Set HTMLDoc = IE.Document
        HTMLDoc.getElementById("uh-search-box").Value = "Earth"
        HTMLDoc.getElementById("uh-search-button").Click
    
    load:
        WaitIE IE, 10000
        Set logo = HTMLDoc.getElementById("logo")
        If Not logo Is Nothing Then
            logo.Click
        ElseIf logo Is Nothing And n < 4 Then
            n = n + 1
            GoTo load
        End If
        WaitIE IE, 5000
    
        'Set ie to Nothing
        IE.Quit
        Set IE = Nothing
    End Sub
    Sub WaitIE(IE As Object, Optional time As Long = 250)
    'Code from: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
    Dim i As Long
    Do
        Sleep time
        Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.READYSTATE = 4) & _
                    vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
        i = i + 1
    Loop Until IE.READYSTATE = 4 Or Not IE.Busy
    End Sub
    

    原答案

    尝试在代码末尾关闭ie并添加一些延迟,因为如果您使用F8单步执行代码,您会看到它可以工作:

    Sub InternetPractice2()
    
    Dim ie As InternetExplorer
    
    Set ie = New InternetExplorer
    
    ie.Visible = True
    
    ie.navigate "https://www.yahoo.com"
    
    Do While ie.Busy = True Or ie.READYSTATE <> 4: DoEvents: Loop
    
    ie.Document.getElementById("uh-search-box").Value = "Earth"
    ie.Document.getElementById("uh-search-button").Click
    'Add delay After .Click
    
    Do While ie.Busy = True Or ie.READYSTATE <> 4: DoEvents: Loop
    Application.wait Now+Timevalue("00:00:05")
    
    ie.Document.getElementById("logo").Click
    'Set ie to Nothing
    Set ie = Nothing
    End Sub
    

    同时关闭 Windows 任务管理器上的所有 iexplorer.exe。

    【讨论】:

    • 丹尼尔,我很快就联系上了。当我逐步浏览代码(使用F8)时它可以工作,但是当我正常运行它时仍然无法工作。有什么想法吗?
    • 丹尼尔,感谢您的帮助。这绝对是一个时机问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2012-08-24
    • 2016-04-20
    • 1970-01-01
    • 2015-04-13
    • 2017-06-02
    相关资源
    最近更新 更多