Excel 等待下载栏出现,然后继续代码
我正在尝试从网站自动下载数据。我目前正在尝试的是,在按下alt+S之前,等待下面显示的下载弹出窗口出现: 完成此操作后,我希望vba等待下载完成弹出窗口出现,然后再继续此过程 到目前为止,我已经尝试了下面的代码来尝试识别并等待下载栏,但是FindInitDownloadPopup()和FindDownloadPopup2()即使在下载栏出现时也不会退出循环。我看过其他类似的帖子,但是没有一个提议的解决方案适合我Excel 等待下载栏出现,然后继续代码,excel,vba,web-scraping,Excel,Vba,Web Scraping,我正在尝试从网站自动下载数据。我目前正在尝试的是,在按下alt+S之前,等待下面显示的下载弹出窗口出现: 完成此操作后,我希望vba等待下载完成弹出窗口出现,然后再继续此过程 到目前为止,我已经尝试了下面的代码来尝试识别并等待下载栏,但是FindInitDownloadPopup()和FindDownloadPopup2()即使在下载栏出现时也不会退出循环。我看过其他类似的帖子,但是没有一个提议的解决方案适合我 Option Explicit Public Declare Function
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String,
ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Sub FindInitDownloadPopup()
Dim ie As InternetExplorer
Dim h As Long
h = ie.hWnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
If h = 0 Then
Do While h = 0
Application.Wait (Now + TimeValue("00:00:02"))
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
Loop
Else
End If
End Sub
Sub FindDownloadPopup2()
Dim hWnd As Long
Do
hWnd = FindWindow("#32770", "File Download")
DoEvents
Loop Until hWnd
End Sub
我也有类似的问题。我的解决方案是在运行代码之前清除downloads文件夹,然后创建一个简单的循环来查找已完成的下载 这里有“点击保存等待下载”的代码。注意:我使用了
睡眠
,因为我发现由于与内部intranet的连接问题,我需要等待。此外,我还根据ClaimNumber
(我在财务部门工作)运行此代码,因此在需要时使用ommit
' Declare Sleep
#If VBA7 Then
Public Declare PtrSafe Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As LongPtr) ' For 64 Bit Systems
#Else
Public Declare Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long) ' For 32 Bit Systems
#End If
Private Sub ClickSave(ClaimNumber As String)
Sleep 1000
Dim o As IUIAutomation
Set o = New CUIAutomation
Do
Dim h As Long
h = IE.HWND
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
If h <> 0 Then
Dim count As Long
count = 0
Exit Do
Else
Sleep 100
count = count + 1
If count = 50 Then Exit Sub
End If
Loop
Dim e As IUIAutomationElement
Set e = o.ElementFromHandle(ByVal h)
Dim iCnd As IUIAutomationCondition
Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
Dim Button As IUIAutomationElement
Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
Do
On Error Resume Next
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
If Err.Number = 0 Then
On Error GoTo 0
Exit Do
Else
Sleep 100
count = count + 1
End If
On Error GoTo 0
Loop Until count = 100
InvokePattern.Invoke
Do
Sleep 1000
Completed = DownloadComplete(ClaimNumber)
If Completed = "Yes" Then Exit Do
Loop
SendMessage h, WM_CLOSE, 0, 0
End Sub
Private Function DownloadComplete(ClaimNumber As String) As String
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
Dim Username As String
Username = Environ("username")
Dim DownloadFolder As String
DownloadFolder = "C:\Users\" & Username & "\Downloads"
Debug.Print DownloadFolder
Dim Folder As Scripting.Folder
Set Folder = FSO.GetFolder(DownloadFolder)
On Error Resume Next
Dim File As Scripting.File
For Each File In Folder.Files
If File.name Like "*" & ClaimNumber & "*" Then
If Err.Number = 0 Then
Completed = "Yes"
Else
Completed = "No"
End If
On Error GoTo 0
End If
Next File
DownloadComplete = Completed
End Function
'声明睡眠
#如果是VBA7,则
公共声明PtrSafe子睡眠库“kernel32”_
(ByVal dwas LongPtr)“”用于64位系统
#否则
公共声明子睡眠库“内核32”_
(ByVal dw毫秒长)用于32位系统
#如果结束
私有子单击保存(ClaimNumber作为字符串)
睡1000
Dim o As IUIAutomation
设置o=新CUI自动化
做
暗h一样长
h=IE.HWND
h=FindWindowEx(h,0,“帧通知栏”,vbNullString)
如果h0那么
不算长
计数=0
退出Do
其他的
睡100
计数=计数+1
如果计数=50,则退出Sub
如果结束
环
作为IUIAutomationeElement的尺寸e
设置e=o.ElementFromHandle(ByVal h)
Dim iCnd作为IUIAutomation条件
设置iCnd=o.CreatePropertyCondition(UIA_NamePropertyId,“保存”)
调暗按钮作为IUIAutomationeElement
设置按钮=e.FindFirst(树范围\子树,iCnd)
做
出错时继续下一步
Dim InvokePattern作为IUIAutomationInvokePattern
设置InvokePattern=Button.GetCurrentPattern(UIA_InvokePatternId)
如果Err.Number=0,则
错误转到0
退出Do
其他的
睡100
计数=计数+1
如果结束
错误转到0
循环直到计数=100
InvokePattern.Invoke
做
睡1000
完成=下载完成(索赔编号)
如果已完成=“是”,则退出Do
环
发送消息h,WM_关闭,0,0
端接头
私有函数下载完成(ClaimNumber作为字符串)作为字符串
将FSO设置为文件系统对象
Set FSO=新文件系统对象
将用户名设置为字符串
用户名=环境(“用户名”)
Dim DownloadFolder作为字符串
DownloadFolder=“C:\Users\”用户名和“\Downloads”
调试.打印下载文件夹
将文件夹设置为脚本。文件夹
Set Folder=FSO.GetFolder(下载文件夹)
出错时继续下一步
将文件设置为脚本.File
对于文件夹.Files中的每个文件
如果File.name像“*”和ClaimNumber&“*”那么
如果Err.Number=0,则
Completed=“是”
其他的
Completed=“否”
如果结束
错误转到0
如果结束
下一个文件
下载完成=完成
端函数
如果用户没有使用可怕的IE浏览器该怎么办?我尝试了与您的代码类似的东西,但是在h0部分,即使出现下载弹出窗口,它也不会退出循环:(没关系,我让它工作了。非常感谢您的帮助!!!:D