簡體   English   中英

如何在 Internet Explorer 彈出窗口中按保存?

[英]How To Press Save on Internet Explorer Popup?

如何操作以下 Internet Explorer 下載彈出窗口。
圖片

我嘗試了下面的代碼,但這不是框架通知欄,所以它沒有幫助。

Sub PressSave(ie As InternetExplorer)
    Dim o As IUIAutomation
    Dim e As IUIAutomationElement
    Application.Wait (Now + TimeSerial(0, 0, 4))
    Set o = New CUIAutomation
    Dim h As Long
    h = ie.hwnd
    'find notification bar
    h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
    If h = 0 Then Exit Sub
    
    'find save button
    Set e = o.ElementFromHandle(ByVal h)
    Dim iCnd As IUIAutomationCondition
    Dim Button As IUIAutomationElement
    Do Until Not Button Is Nothing
        Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
        Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
    Loop
    
    'click save
    Dim InvokePattern As IUIAutomationInvokePattern
    Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
    InvokePattern.Invoke
    
    'wait till file is completely downloaded
    Dim iOpen As IUIAutomationCondition
    Dim OpenButton As IUIAutomationElement
    Do Until Not OpenButton Is Nothing
        Set iOpen = o.CreatePropertyCondition(UIA_NamePropertyId, "View 
downloads")
        Set OpenButton = e.FindFirst(TreeScope_Subtree, iOpen)
    Loop
End Sub

我嘗試使用 API 來獲取數據,但一直收到錯誤 401,這意味着它需要身份驗證,我不確定是否可以添加參數來實現。

Sub APIToGetData()
Dim sURL As String
Dim savePath As String
Dim WinHttpReq As Object

savePath = "C:\Downloads"
sURL = "https://[myurlhere]/api/admin/data_dumps/download"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")

WinHttpReq.Open "GET", sURL, False
WinHttpReq.send

If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.responseBody
    oStream.Close
End If
End Sub

如何在不使用 SendKeys 的情況下操作此彈出窗口?

大約一年前,我遇到了類似的問題,我可以通過實現 API 來解決

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal Msg As Integer, ByVal wParam As Long, ByVal lParam As Long) As Long

使用 FindWindow API 我實現了一種監視程序

Do Until FindWindow(vbNullString, "Internet Explorer") > 0
    Sleep 100
    DoEvents
Loop

找到窗口后,我讓它確認默認選擇(在我的情況下為“打開”),直到窗口消失

Do Until FindWindow(vbNullString, "Internet Explorer") = 0
    SendMessage FindWindow(vbNullString, "Internet Explorer"), WM_COMMAND, IDOK, 0
    Sleep 100
    DoEvents
Loop

SendMessage 很棒,因為它將命令直接發送到目標 hWnd,而不是 SendKeys,后者只將指定的擊鍵發送到任何焦點。

Tate Garringer 的回答有正確的邏輯,但由於某種原因對我不起作用。 我最終找到了一些有用的東西,以及大量有用的信息: https : //www.mrexcel.com/forum/excel-questions/502298-need-help-regarding-ie-automation-using-vba- 3.html

以下是對我的問題的直接回答:

'Find the Save As window, waiting a maximum of 10 seconds for it to appear
timeout = Now + TimeValue("00:00:10")
Do
    hwnd = FindWindow("#32770", "Save As")
    DoEvents
    Sleep 300
Loop Until hwnd Or Now > timeout

希望這對其他人有幫助

暫無
暫無

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

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