簡體   English   中英

VBA 從 IE 下載文件:單擊“保存”后出現第二個彈出窗口

[英]VBA downloading files from IE: Second popup appears after clicking "Save"

我正在自動化一個過程,我必須從 Internet Explorer 中的網站下載文件 我有以下代碼,一旦下載彈出,就會單擊“保存”按鈕。

Sub thing()

'Find download message and click Save to download file
Dim o As IUIAutomation
Set o = New CUIAutomation
Dim Completed As String
Dim h As Long
Dim ie As InternetExplorer
Dim html As HTMLDocument

Sleep 1000
Set ie = CreateObject("InternetExplorer.application")
Do
    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
    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)

Sleep 1000

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

InvokePattern.Invoke

Do
    Sleep 1000
    Completed = DownloadComplete()
    If Completed = "Yes" Then
        Exit Do
    Else
    End If
Loop

SendMessage h, WM_CLOSE, 0, 0

End Sub

但是,當我在家中運行此自動化時,有時會在單擊“保存”后出現以下彈出窗口: 對話框

如果此按鈕彈出,我應該如何告訴 VBA 單擊“重試”? 感謝您的幫助!

由於這是第二個彈出通知欄,您無法調用代碼中的Button AutomationElement Button ,因為在 IE 中找不到“保存”按鈕。

您需要將AutomationCodition iCnd重置為“重試”並找到調用它的按鈕。 如果您找不到保存按鈕, Button將為Nothing

While Not filefound
    If Dir(file_path) <> "" Then filefound = True
    Application.Wait (Now + TimeValue("0:00:01"))

    Dim o As IUIAutomation
    Dim e As IUIAutomationElement
    Set o = New CUIAutomation

    Dim h As Long
    h = ie.Hwnd
    h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)

    If h <> 0 Then
        On Error Resume Next

        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)
        If Button Is Nothing Then
            Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Retry")
            Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
        End If

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

        On Error GoTo 0
    End If
Wend

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM