【问题标题】:VBA IE approve download in dialog windowVBA IE 在对话框窗口中批准下载
【发布时间】:2016-09-26 12:41:13
【问题描述】:

尝试使用文件链接下载数据。 IE 打开并导航到文件,但弹出窗口要求我打开文件。我需要点击这个打开按钮。请求导航弹出窗口的帮助。到目前为止,这是我的代码:

Sub GetData()

Const cURL = "http://www.bankofengland.co.uk/statistics/Documents/yieldcurve/ukinf05.xlsx"

Dim IE As InternetExplorer

    Dim doc As HTMLDocument

    Dim HTMLelement As IHTMLElement

    Set IE = New InternetExplorer

    IE.Visible = False
    IE.Navigate cURL


End Sub

【问题讨论】:

  • 使用 IE 的开发者工具来定位open 按钮的元素id 或名称,并设置一个对象引用,然后您可以点击它。快速搜索此网站(或 Google)将为您提供数百个示例
  • 如果你有一个直接指向文件的 URL,使用 API 比自动化 IE 容易得多。见this answer
  • @Comintern 不幸的是,我尝试过这样的代码并得到相同的“无法下载文件,或者源 URL 不存在”消息。尽管这是一个有效的链接。
  • @Dave 我使用的代码只打开下载窗口,即使 IE.Visible=True 也只能看到下载窗口。因此我无法打开开发者工具。

标签: vba excel internet-explorer download


【解决方案1】:

正如@Comintern 所建议的那样,稍微检查一下this blog entry by SiddarthRout

Option Explicit

Private Declare Function URLDownloadToFile Lib "urlmon" _
    Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
    ByVal szURL As String, ByVal szFileName As String, _
    ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Dim Ret As Long

Sub Sample()
    Dim strURL As String
    Dim strPath As String

    '~~> URL of the Path
    strURL = "http://www.bankofengland.co.uk/statistics/Documents/yieldcurve/ukinf05.xlsx"
    '~~> Destination for the file
    strPath = "C:\temp\ukinf05.xlsx"

    Ret = URLDownloadToFile(0, strURL, strPath, 0, 0)

    If Ret = 0 Then
        MsgBox "File successfully downloaded"
    Else
        MsgBox "Unable to download the file"
    End If
End Sub

我得到的最终结果是文件,正确地位于我的 C:\temp\ 文件夹中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    相关资源
    最近更新 更多