【问题标题】:VBA - download file from url without filenameVBA - 从没有文件名的url下载文件
【发布时间】:2016-01-28 19:59:59
【问题描述】:

我有一个由 https:// servername/parameterList 组成的 URL。如果我在 IE 中输入 URL,系统会提示我输入用户名/密码,然后可以选择打开/保存查询结果的文本文件。

我要做的是使用 VBA 自动获取查询结果,将其保存到文件或将内容返回到变量。我尝试了几种从 URL 下载文件的方法,包括

How do i download a file using VBA (Without internet explorer)

但是,这会保存一个包含 HTML(关于请求?)的文件,而不是可下载文件中的查询结果。所以我得到的是 !DOCTYPE html 等,而不是制表符分隔列中的搜索结果表。

我认为上面链接中的示例都有一些您想要在 URL 末尾下载的文件,因此它是指向文件/图像的直接链接。

抱歉,如果有解决此问题的问题。我认为我对过程中发生的事情的了解不足以找到有意义的结果。初始 URL 是否重定向?如果结果文件是根据查询动态生成的,有什么办法可以得到吗?谢谢。

【问题讨论】:

    标签: excel vba url download


    【解决方案1】:

    我遇到了和你一样的问题,已经用 SendKeys 下载了文件。这不是一个强大的解决方案,我在让它与多个版本的 IE 一起工作时遇到问题,但它可以完成工作。

    'YourLink is a string of the URL in the form "http://link.com/"
    
    Dim IE As Object
    Dim myURL As String
    
    myURL = YourLink
    
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    
    IE.navigate myURL
    
    SetWindowPos IE.HWND, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
    
    'Wait until the web page is fully loaded.
    
    Sleep 1500 'delay in milliseconds
    
    'If they click out of IE, it won't work. This makes sure that if they did, it just stops right there.
    If GetForegroundWindow() <> IE.HWND Then
        MsgBox "Please try again and do not click out of the internet explorer window."
        Exit Sub
    End If
    
    Sleep 1500
    
    If GetForegroundWindow() <> IE.HWND Then
        MsgBox "Please try again and do not click out of the internet explorer window."
        Exit Sub
    End If
    
    'Press keys to download (tabs to save button and then clicks it)
    
    SendKeys "{TAB}", True
    SendKeys "{TAB}", True
    SendKeys "{ENTER}", True
    SendKeys "{ENTER}", True
    

    我是编程的初学者,这段代码的不同部分取自其他人对其他问题的回答(不幸的是,我没有将它们写下来以得到适当的信任)。要使用 Sleep、GetForegroundWindow() 和 SetWindowPos 函数,您需要找到要放入模块的引用(应该很容易找到)。如果您在发布此问题后找到了更好的解决方案,请告诉我!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-15
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多