gpt4 book ai didi

Excel VBA 从 IE 11 下载栏另存为

转载 作者:行者123 更新时间:2023-12-04 20:09:47 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How do I download a file using VBA (without Internet Explorer)

(6 个回答)


3年前关闭。




我有以下代码可以从网上下载文件,但我必须手动执行 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”对话框。我更改了 FindWindowEXh = FindWindowEx(h, 0, "View Downloads - Internet Explorer", vbNullString)
另存为的文件名和位置需要是
Filename = "C:\Historic_Weather_Data\Precipitation\" & Range("File_Name").Value

我也越来越

Run-time error "-2147467259 (80004005)': Method 'Busy' of object 'IWebBrowser 2' failed"



调试到 While ieApp.Busy线。

任何帮助表示赞赏。

最佳答案

不幸的是,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

关于Excel VBA 从 IE 11 下载栏另存为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56063122/

25 4 0