使用Excel vba完成下载后,关闭IE 9中的下载窗口

时间:2018-01-23 15:50:52

标签: excel-vba internet-explorer automation vba excel

我的代码冻结了DoEvents,并且没有更进一步。我试图在下载完成后关闭浏览器。请咨询。

Dim hWnd As LongPtr
Dim timeout As Date
Dim fullfilename As String
Dim AutomationObj As IUIAutomation
Dim WindowElement As IUIAutomationElement

'Find the Download complete window, waiting a maximum of 1 minutes for it to appear.  Timeout value is
'dependent on the size of the download, so make it longer for larger files.

timeout = Now + TimeValue("00:01:00")

Do
    hWnd = FindWindow("#32770", "Download complete")
    DoEvents
    Sleep 200
Loop Until hWnd Or Now > timeout

Debug.Print "   Download complete window "; Hex(hWnd)

If hWnd Then
    oBrowser.Quit
End If`

1 个答案:

答案 0 :(得分:0)

我用不同的方法得到了结果。发布答案以防有人发现它有用。

Dim AllElements As IUIAutomationElementArray
Dim Element As IUIAutomationElement
Dim InvokePattern As IUIAutomationInvokePattern
Dim iCnd As IUIAutomationCondition
Dim AutomationObj As IUIAutomation
Dim FrameElement As IUIAutomationElement
Dim bFileExists As Boolean
Dim hwnd As LongPtr
Dim success As Boolean
'create the automation object
Set AutomationObj = New CUIAutomation
success = False
WaitSeconds 3

'get handle from the browser
hwnd = oBrowser.hwnd

'get the handle to the Frame Notification Bar
hwnd = FindWindowEx(hwnd, 0, "Frame Notification Bar", vbNullString)
If hwnd = 0 Then Exit Sub

'obtain the element from the handle
Set FrameElement = AutomationObj.ElementFromHandle(ByVal hwnd)
timeout = Now + TimeValue("00:05:00")
Do
  'Get split buttons elements
   Set iCnd = AutomationObj.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_SplitButtonControlTypeId)
   Set AllElements = FrameElement.FindAll(TreeScope_Subtree, iCnd)

   'There should be only 2 split buttons only
   If AllElements.length = 2 Then
        success = True
   End If
Loop Until success = True Or Now > timeout

If success Then
oBrowser.Quit
End If