【问题标题】:Excel VBA to Save As from IE 11 Download Bar [duplicate]Excel VBA从IE 11下载栏另存为[重复]
【发布时间】:2019-09-27 12:12:10
【问题描述】:

我有以下代码可以从网上下载文件,但我必须手动执行Save As

  Dim Filename As String
  Dim ieApp As Object
  Dim URL As String

    URL = Range("All_Quad_URL")
    Filename = "C:\Historic_Weather_Data\Precipitation\" & Range("File_Name").Value

    Set ieApp = CreateObject("InternetExplorer.Application")
    ieApp.Visible = True
    ieApp.Navigate URL

      While ieApp.Busy Or ieApp.ReadyState <> 45
        DoEvents
      Wend

    ieApp.Quit

    Set ieApp = Nothing

我想自动化Save As。我试过以下没有运气:

Controlling IE11 "Do you want to Open/Save" dialogue window buttons in VBA

我仍然看到带有打开/保存选项的“查看下载 - Internet Explorer”对话框。我把FindWindowEX改成了h = FindWindowEx(h, 0, "View Downloads - Internet Explorer", vbNullString)

另存为的文件名和位置需要是

Filename = "C:\Historic_Weather_Data\Precipitation\" & Range("File_Name").Value

我也来了

运行时错误“-2147467259 (80004005)”:对象“IWebBrowser 2”的方法“忙碌”失败”

调试到While ieApp.Busy 行。

感谢任何帮助。

【问题讨论】:

  • 网址是什么?
  • 假设 URL 是可下载文件的直接链接,请尝试不使用 IE。使用答案中给出的 URLDownloadToFile API 函数:stackoverflow.com/a/23299764/1467082
  • 谢谢GSerg!!!成功了!!!

标签: excel vba


【解决方案1】:

不幸的是,IE 不允许您设置保存下载的路径。我已经搜索了几个小时,直到找到答案。它将保存到您上次保存下载的位置。

不过,我有一些好消息。单击下载按钮后放置以下代码,它应该适合您。您还需要添加适当的库引用。

编辑:如果你想保存完整的代码,你可以找到它Here

'wait for save as window to appear
Dim o As IUIAutomation
Dim h As LongPtr
Set o = New CUIAutomation
h = 0
Do Until h > 0
    'h = ie.hWnd
    h = FindWindow("#32770", "Internet Explorer")
Loop

'find and click save as button
Dim e As IUIAutomationElement
Dim iCnd As IUIAutomationCondition
Dim Button As IUIAutomationElement
Set e = o.ElementFromHandle(ByVal h)
Set Button = Nothing
Do Until Not Button Is Nothing
    Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save as")
    Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
Loop

Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多