如何循环从href下载excel(当所有文件的href相同时)

时间:2019-04-19 18:44:17

标签: excel vba

以下载为目标的目标是通过href。但是,无法下载所有具有循环功能的Excel(当href链接看起来相同时)。在这种情况下会导致什么?下载这些文件后,如何将每个excel保存在本地C文件夹中?

这是必需的HTML href链接

<td class="tenderlink">
<a href='/Documents/ProcurementDisposal/20190419102042818.xls' 
         target="_blank">View</a></td>

代码:

Sub download()
 'Application.ScreenUpdating = False
    Dim ie As InternetExplorer
    Dim ele As Object

    Set ie = New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "http://www.nafed-india.com/Home/ProcDispoDetails"

        While .Busy Or .readyState < 4: DoEvents: Wend
    End With
    For Each ele In ie.document.querySelector("a[href^='/Documents/ProcurementDisposal']").Click
        Next ele
End Sub

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

Sub download()

    Dim ie As InternetExplorer
    Dim el As Object, els As Object

    Set ie = New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "http://www.nafed-india.com/Home/ProcDispoDetails"

        While .Busy Or .readyState < 4: DoEvents: Wend
    End With

    'Note: querySelectorAll unless you only want one element
    Set els = ie.document.querySelectorAll("a[href^='/Documents/ProcurementDisposal']")
    For Each el In els
        Debug.Print el.href '<< pass this to Workbook.Open() then you can save the
                            '    workbook where you need
    Next


End Sub