当仅提交按钮时,从Web下载文件时收到错误

时间:2016-02-11 12:04:01

标签: vba excel-vba excel

我需要从网上下载文件,但只有提交按钮 没有网址直接到达文件。
我试图通过它的id捕获按钮,但是VBA丢弃了运行时错误'424'(需要对象) 这是代码:

Sub keler_submit()  
    Application.ScreenUpdating = False  
    Set IE = CreateObject("InternetExplorer.Application")  
    IE.navigate "https://www.keler.hu/T%C3%A1rsas%C3%A1gi%20esem%C3%A9nyek/"  
    While IE.Busy  
        DoEvents  
    Wend  
    Set doc = IE.document  
    gomb = doc.getElementById("ctl00_wpm_UserControlPortlet1682286671_ctl00_wpm_UserControlPortlet1682286671_userControlPortlet_DownloadButton1")  
    gomb.submit  
    Set IE = Nothing  
    Application.ScreenUpdating = True  
End Sub  

提前谢谢

2 个答案:

答案 0 :(得分:0)

这些行不正确:

gomb = doc.getElementById("ctl00_wpm_UserControlPortlet1682286671_ctl00_wpm_UserControlPortlet1682286671_userControlPortlet_DownloadButton1")  
gomb.submit

doc.getElementById返回一个对象类型,因此您需要使用Set将其分配给变量。然后,应单击您尝试访问的元素而不是提交。 IE很可能会显示“您是否要打开或保存此文件”对话框,所以首先让IE对象可见以显示:

IE.visible = True
Set gomb = doc.getElementById("ctl00_wpm_UserControlPortlet1682286671_ctl00_wpm_UserControlPortlet1682286671_userControlPortlet_DownloadButton1")  
gomb.click

答案 1 :(得分:0)

您还需要一个循环以确保页面已加载。以下作品:

Option Explicit

Public Sub GetInfo()
    Dim IE As New InternetExplorer

    With IE
        .Visible = True
        .navigate "https://www.keler.hu/T%C3%A1rsas%C3%A1gi%20esem%C3%A9nyek/"

        While .Busy Or .readyState < 4: DoEvents: Wend
        Do
            Dim b As Object
            On Error Resume Next
            Set b = .document.getElementById("ctl00_wpm_UserControlPortlet1682286671_ctl00_wpm_UserControlPortlet1682286671_userControlPortlet_DownloadButton1")
            On Error GoTo 0
        Loop While b Is Nothing

        b.Click
        'Handle save button

        'Quit '<== Remember to quit application
    End With
End Sub