使用vba打开IE并下载.csv文件?

时间:2017-04-12 17:41:48

标签: vba excel-vba internet-explorer excel

我有一个程序,必须打开一个网页,复制一些数据,保存数据,并关闭网页。 This is the format of the webpage I am looking for。我的目标是让vba自动下载.csv文件(页面右侧,数据本身上方,如果您没有看到它),并将其移动到下载的特定位置。

(之前我使用的是workbooks.open(URL),但我将网址更改为政府网站,以获得更好,更可靠的数据。问题是,当我这样做时,这个网址似乎不起作用,不像这个旧网站会打开Excel中的页面。这很方便,如果有人知道如何再次使用这个网站,我会反过来做,但我仍然很好奇知道如何使用IE for vba)

问题是,我知道如何通过VBA使用IE。我到目前为止的代码是

Dim IE As InternetExplorer
Dim Doc As Object, Elements As Object 'these two things and the next row are just things I heard might be useful through my research on how to do this
Dim WSSh As Object

Set IE = New InternetExplorer
Set WSSh = CreateObject("WScript.Shell") 'not even sure what to do with this but until I'm sure I won't need it I'll just leave it there

With IE
    .Visible = True 'once it's finished I'll take this out, I don't actually need to see this happen
        'just want to see it happen while I write/debug it
    .Navigate DataURLBegin & DataURLMiddle & DataURLEnd 'these three combined form the url above
    Do
        DoEvents
    Loop While .ReadyState <> READYSTATE_COMPLETE
    Stop
    'this is where I've tried a lot of things but I'm not sure where to even start
End With

正如你所看到的那样......我们已经走得很远了。我从其他人那里尝试做类似的事情,我必须循环浏览可以激活的工作表上的元素,但我浏览了IE的孩子,我没有看到任何东西这似乎是元素,或者我可以循环。所以我不完全确定从哪里开始。

编辑:所以我觉得我已经接近了,但我还是遇到了麻烦。我的代码现在是:

Option Explicit

Sub ContactWeb(ByVal URL As String, ByVal DownloadPath As String)
'I moved it into its own sub to stop taking up so much space
'and make things easy to read

Dim IE As InternetExplorer 'some of these aren't used, I'll clean it up once this works
Dim Doc As Object, Elmt As Object
Dim HTMLColl As MSHTML.IHTMLElementCollection
Dim HTMLInput As MSHTML.HTMLInputElement

Set IE = New InternetExplorer

With IE
    .Visible = True
    .Navigate URL

    Do  'waits until the webpage is fully loaded, otherwise everything afterwards fails
        DoEvents
    Loop While .readyState <> READYSTATE_COMPLETE

    Set Doc = IE.Document

    Set Elmt = Doc.getElementById("bulkCsvLink")

    Elmt.Click 'I can't tell if this is actually doing anything, though...

    Do
        DoEvents
    Loop While .readyState <> READYSTATE_COMPLETE

    Stop

    Set HTMLColl = Doc.getElementsByTagName("input")
    For Each Elmt In HTMLColl
        If Elmt.innerText = "Download Data" Or Elmt.Value = "Download Data" Then
            Stop
            Elmt.Click 'This I know works because I tested it on the xml version
            Exit For
        End If
    Next Elmt

    Stop
    Do
        DoEvents
    Loop While .readyState <> READYSTATE_COMPLETE
    IE.Quit
End With

End Sub

尽可能接近,这段代码是正确的,但我不知道如何确认底部的小消息,询问我是否要下载(我可以发誓不是这样的#t; t出现在之前,但也许那只是我;对任何看到我所做的一系列编辑的人都感到抱歉。我如何点击确认?最好能够输入下载位置,但我想我总是可以使用代码将其复制/粘贴到正确的位置。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您需要下载CSV文件并在Excel中打开它。例如,如果您使用Chrome手动执行此操作,则可以看到其来源的URL是:

http://climate.weather.gc.ca/climate_data/bulk_data_e.html?format=csv&stationID=889&Year=2011&Month=9&Day=19&timeframe=1&submit=Download+Data

您可以继续使用Workbooks.Open(URL)或使用XMLHTTP对象甚至是QueryTables,只需使用您需要的参数(工作站ID,日期等)格式化URL字符串。不需要IE - 我只是在使用auth,按钮点击或获取数据所需的JavaScript时尝试使用它。

<强> UPD: 使用下面的宏,无误地工作

Sub OpenCSV()
strURL = "http://climate.weataher.gc.ca/climate_data/bulk_data_e.html?format=csv&stationID=889&Year=2011&Month=9&Day=19&timeframe=1&submit=Download+Data"
Application.Workbooks.Open (strURL)
End Sub